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

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

发表于 2015-08-29   |   分类于 Go   |   暂无评论git

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

方法一:homebrew

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

1
2
3
4
brew update && brew upgrade
brew install git
brew install mercurial
brew install go

这样安装以后经过命令行输入go就能够看到相关的信息。输入go env查看环境信息:bash

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等查看是否安装。工具

spa

环境变量配置

1.0 查看是否存在bash_profile命令行

1
cd ~/.bash_profile

2.0 若是不存在则建立bash_profile Mac环境配置文件code

1
vim ~/.bash_profile

3.0 添加go 环境变量
若是是第一种安装方法,只须要指定一下GOPATH便可。为了让本身的程序编译以后在命令行任何地方能直接执行,再加入GOPATH下的bin便可:blog

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

复制粘贴保存便可。

相关文章
相关标签/搜索