您的位置:首页 > 编程语言 > Go语言

Mac下安装Go和配置相应环境

2017-07-07 00:00 621 查看
上的教程很多,但是并不完整,使得我在安装的过程中遇到了各种问题。后来综合几个教程给配置好了。我采用了两种方法,后来去掉了其中的一种。下面是我的安装步骤:

方法一:homebrew

homebrew
是Mac系统下面目前使用最多的管理软件的工具,目前已支持Go,可以通过命令直接安装Go,为了以后方便,应该把
git
mercurial
也安装上:

1
2
3
4
brew update && brew upgrade
brew install git
brew install mercurial
brew install go
这样安装之后通过命令行输入
go
就可以看到相关的信息。输入
go env
查看环境信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH=""
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"

方法二:pkg包安装

直接去官方下载安装包,然后双击安装,之后同样地输入
go
go env
go version
等查看是否安装。



环境变量配置

1.0 查看是否存在bash_profile

1
cd ~/.bash_profile
2.0 如果不存在则创建bash_profile Mac环境配置文件

1
vim ~/.bash_profile
3.0 添加go 环境变量
如果是第一种安装方法,只需要指定一下
GOPATH
即可。为了让自己的程序编译之后在命令行任何地方能直接执行,再加入
GOPATH
下的
bin
即可:

1
2
3
4
5
6
7
8
#This is my personal bash_profile,when loaded at login.
#===2015-08-15===

#GOPATH
export GOPATH=$HOME/Documents/go_workspace

#GOPATH bin
export PATH=$PATH:$GOPATH/bin
但是第二种方法安装之后输入
go
会显示
ommand not found: go
,所以需要在
.bash_profile
中指定
GOROOT
下的
bin


1
2
3
4
5
6
7
8
9
10
11
12
13
#This is my personal bash_profile,when loaded at login.
#===2015-08-15===
#GOROOT
export GOROOT=/usr/local/go

#GOPATH
export GOPATH=$HOME/Documents/go_workspace

#GOROOT bin
export PATH=$PATH:$GOROOT/bin

#GOPATH bin
export PATH=$PATH:$GOPATH/bin
一般环境变量更改后,重启后生效。在重启终端的时候就会自动执行
.bash_profile
文件。

如果想立刻生效,则可执行下面的语句:

1
$ source .bash_profile

问题:

如果打开终端没有生效,就把上面的追加到
~/.zshrc
中:

1
open -e ~/.zshrc
复制粘贴保存即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  go mac 安装