为了能够愉快地进行golang编程,我们需要安装以下几样东西:
- 包管理Homebrew
- 语言环境golang
- 版本管理git
- 虚拟器docker
编译器Goland
我将按照这个顺序叙述整个安装过程docker 其实是可选的,它可以把应用程序打包为可移植的、自给自足的容器。这样一来,你就可以在本地生成golang程序的docker镜像,直接扔到测试环境,便可以进行测试了,不需要再进行代码上传,环境配置等操作了。
如果你觉得暂时用不到,也可以先不装。
## 1 安装brew
Homebrew有点类似于Linux操作系统中的apt-get(Ubuntu)、yum(yum),Mac的操作系统中使用它解决包依赖问题,套用官方的话来说:
Homebrew 能干什么?
使用 Homebrew 安装 Apple 没有预装但 你需要的东西。
让我们开始安装吧!
在命令行输入以下指令
fabric:~ fabric$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
出现以下内容,安装成功
==> Installation successful!
==> Homebrew has enabled anonymous aggregate user behaviour analytics.
Read the analytics documentation (and how to opt-out) here:
https://docs.brew.sh/Analytics.html
==> Next steps:
- Run `brew help` to get started
- Further documentation:
https://docs.brew.sh
## 2 安装并配置golang
2.1 方法一:通过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=""
GOHOS