AbelSu教你搭建go语言开发环境

时间:2023-03-08 21:56:56
AbelSu教你搭建go语言开发环境

AbelSu教你搭建go语言开发环境

go语言官网:https://golang.org/

windows:
官网下载go1.6.windows-amd64.msi安装文件,安装位置选择默认C:\Go\
安装结束后配置环境变量Path: C:\Go\bin;
接着创建一个GOPATH环境变量,这个变量很重要,我自己写的代码要放到这个变量中配置的目录中,go编译器才会找到并编译;
继续创建一个GOROOT变量,配合go编译器安装的目 GOROOT:C:\Go\;
完成以上步骤后,打开命令行输入 # go version,就会出现go编译器的版本号。
输入# go env就可以看到正确的go环境。

Linux:
Linux部署golang,首先请在windows上自行安装SSH远程工具,比如Xshell4等。
1.安装mercurial包(mercurial版本管理系统,可以输出hg名字检测是否安装)
# sudo apt-get install mercurial 或者# sudo easy_install mercurial
2.安装git
# sudo apt-get install git
3.安装gcc
# sudo apt-get install gcc
4.下载golang的压缩包
# wget https://storage.googleapis.com/golang/go1.6.linux-amd64.tar.gz
# tar -zxvf go1.6.linux-amd64.tar.gz -C /home
将解压的文件夹改名为go
添加环境变量: # vi /etc/profile
在profile里面添加如下内容
export GOROOT=/home/go
export PATH=$GOROOT/bin:$PATH
export GOPATH=/home/gopkg
GOPATH和GOROOT修改为你的路径即可.
然后我们刷新环境变量: #source /etc/profile
最后我们验证一下是否安装成功: # go version
5.我们也可以不下载golang的压缩包来进行安装
# sudo apt-get install golang
# go version

Mac:
官网下载go1.6.darwin-amd64.pkg安装文件进行安装
或者安装homebrew后输入命令:
# brew update
# brew install mercurial  (注意:mercurial和当前广泛应用的git和svn都是源码版本管理工具。mercurial 是python编写的,安装前需要先安装好python.)
# brew install go
进行安装。
输入# go version,就会出现go编译器的版本号.
输入# go env就可以看到正确的go环境.

开发工具我选择IDEA + go plugin, (2018年补充:开发工具也可以使用LiteIDE)

相关网站:https://github.com/go-lang-plugin-org/go-lang-idea-plugin

安装go plugin:

File -> Setting ,在Settings对话框的左侧选择“Plugins”,然后点击“Browse repositories”,

在Settings对话框的左侧选择“Plugins”,然后点击“Browse repositories”,点击下面的“Manage respositories”按钮。

在“Custom Plugin Repositories”对话框中,点击“+”按钮,

在“Add Repository”对话框中,输入go语言插件的地址:https://plugins.jetbrains.com/plugins/alpha/5047,并点击OK按钮。

在“Custom Plugin Repositories”对话框,点击OK按钮。

接着,在“Browse Repositories”对话框的左侧找到“Go“的插件,并点击”Install“按钮。

重启后,就完成了Go语言插件的安装了。

File -> Project Structure,在SDKs那一项添加Go SDK

AbelSu教你搭建go语言开发环境

开始创建第一个go语言程序并运行:

AbelSu教你搭建go语言开发环境

ok, go语言开发环境到这里就搭建完成,enjoy it~