1、安装Homebrew

安装步骤,参考Homebew

2、安装Golang

1
$ brew install go

2.1、查看Golang安装信息

1
$ brew info go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
go: stable 1.9.2 (bottled), devel 1.10beta1, HEAD
Open source programming language to build simple/reliable/efficient software
https://golang.org
/usr/local/Cellar/go/1.8.3 (7,035 files, 282.0MB)
  Poured from bottle on 2017-09-05 at 09:29:02
/usr/local/Cellar/go/1.9 (7,639 files, 293.7MB)
  Poured from bottle on 2017-09-05 at 09:45:50
/usr/local/Cellar/go/1.9.2 (7,646 files, 293.9MB) *
  Poured from bottle on 2018-01-09 at 16:50:46
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/go.rb
==> Requirements
Build: git ✔
Required: macOS >= 10.8 ✔
==> Options
--without-cgo
	Build without cgo (also disables race detector)
--without-race
	Build without race detector
--devel
	Install development version 1.10beta1
--HEAD
	Install HEAD version
==> Caveats
A valid GOPATH is required to use the `go get` command.
If $GOPATH is not specified, $HOME/go will be used by default:
  https://golang.org/doc/code.html#GOPATH

You may wish to add the GOROOT-based install location to your PATH:
  export PATH=$PATH:/usr/local/opt/go/libexec/bin

2.3、设置GOROOT、GOPATH

编辑配置文件~/.zshrc, 更新内容:

1
2
3
export GOPATH=/Users/xiaoxiwang/go:/Users/xiaoxiwang/Documents/demo/golangDemo
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:/Users/xiaoxiwang/anaconda3/bin:/usr/local/opt/go/libexec/bin

保存修改,更新配置

1
source ~/.zshrc

2.4、安装完成

查看安装的golang信息:

1
$ go env	
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/xiaoxiwang/go:/Users/xiaoxiwang/Documents/demo/golangDemo"
GORACE=""
GOROOT="/usr/local/opt/go/libexec"
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/2s/5xlkp2f149j5bydw1rhxhyfw0000gn/T/go-build390073932=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"