轉載自:https://www.jianshu.com/p/79bdd20c46cf
安裝並配置golang
通過brew安裝golang
- 首先看看有哪些golang版本可用
fabric:~ fabric$ brew search go
==> Formulae
algol68g go-jira gofabric8 goolabs gx-go [email protected]
arangodb go-statik goffice goose Hugo mongoose
argon2 [email protected] gollum gopass jfrog-cli-go pango
bogofilter [email protected] golo gor jpegoptim pangomm
cargo-completion [email protected] gom goreleaser lego percona-server-mongodb
certigo goaccess gomplate gost lgogdownloader pygobject
cgoban goad goocanvas gosu libgosu pygobject3
clingo gobby goofys gotags mongo-c-driver ringojs
django-completion gobject-introspection google-authenticator-libpam goto mongo-cxx-driver spaceinvaders-go
forego gobuster google-benchmark gource mongo-orchestration spigot
fuego gocr google-java-format govendor mongodb svgo
gnu-go gocryptfs google-sparsehash gowsdl [email protected] wego
go godep google-sql-tool gox [email protected] wireguard-go
go-bindata goenv googler gst-plugins-good [email protected]
我們發現最新的有1.9可以使用
- 安裝brew下最新版本的go
fabric:~ fabric$ brew install [email protected]
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/[email protected]_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring [email protected]_sierra.bottle.tar.gz
==> 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/[email protected]/libexec/bin
This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have this software first in your PATH run:
echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.bash_profile
==> Summary
/usr/local/Cellar/[email protected]/1.9.7: 7,668 files, 294.2MB
- 配置golang的相關環境變量
fabric:~ fabric$ vim ~/.bashrc
將下面內容添加進上面的文件
#GOROOT
export GOROOT=/usr/local/opt/go\@1.9
#GOPATH
export GOPATH=$HOME/Documents/code/gopath
#GOPATH root bin
export PATH=$PATH:$GOROOT/bin
GOPATH可以根據個人習慣設置爲其他目錄
本人習慣在home目錄下的Documents裏新建一個code目錄,用於存放各種語言的代碼,比如Documents/code/gopath用於存放golang的代碼,Documents/code/www用於存放php代碼...
讓改動生效
fabric:~ fabric$ source ~/.bashrc
- 試一試golang是否安裝成功
出現以下內容,則安裝成功
fabric:~ fabric$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/fabric/Documents/code/gopath"
GORACE=""
GOROOT="/usr/local/opt/go\@1.9"
GOTOOLDIR="/usr/local/Cellar/[email protected]/1.9.7/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/wc/bby1pbz17v3dkr8rmcpjptwm0000gn/T/go-build871394220=/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"
作者:夏之繪
鏈接:https://www.jianshu.com/p/79bdd20c46cf
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯繫作者獲得授權並註明出處。