Mac下安装go lang

转载自:https://www.jianshu.com/p/79bdd20c46cf

安装并配置golang

通过brew安装golang

  • 首先看看有哪些golang版本可用
fabric:~ fabric$ brew search go
==> Formulae
algol68g                       go-jira                        gofabric8                      goolabs                        gx-go                          mongodb@3.6
arangodb                       go-statik                      goffice                        goose                          Hugo                           mongoose
argon2                         go@1.4                         gollum                         gopass                         jfrog-cli-go                   pango
bogofilter                     go@1.8                         golo                           gor                            jpegoptim                      pangomm
cargo-completion               go@1.9                         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                         mongodb@3.0                    wego
go                             godep                          google-sql-tool                gox                            mongodb@3.2                    wireguard-go
go-bindata                     goenv                          googler                        gst-plugins-good               mongodb@3.4  

我们发现最新的有1.9可以使用

  • 安装brew下最新版本的go
fabric:~ fabric$ brew install go@1.9
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/go@1.9-1.9.7.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring go@1.9-1.9.7.high_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/go@1.9/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/go@1.9/bin:$PATH"' >> ~/.bash_profile

==> Summary
  /usr/local/Cellar/go@1.9/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/go@1.9/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
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

-------------------------------------------------------------------------------------------------------------------------------

后来发现每次终端打开要使用 golang 的时候都需要执行 source ~/.bashrc 命令,为了解决这个问题:

vim ~/.bash_profile 在文件加入下面内容

#加载.bashrc文件 是为了解决每次打开终端都要执行source ~/.bashrc命令 go的配置才生效的问题
if test -f .bashrc ; then
source .bashrc
fi

解决方案来自:https://www.jianshu.com/p/c4946024b946

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值