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

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

方法一: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包安装

直接去官方下载安装包,然后双击安装,之后同样地输入gogo envgo 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

复制粘贴保存即可。

转载于:https://my.oschina.net/u/3127059/blog/1204676

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值