为了能够愉快地进行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=""
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"
2.2 方法二:从源码安装golang
这个在我另外一篇文章做了详细的叙述,详情请见《修改并编译golang源码》
3 安装配置git
3.1 用brew安装git
fabric:~ fabric$ brew install git
==> Downloading https://homebrew.bintray.com/bottles/git-2.18.0.high_sierra.bott
######################################################################## 100.0%
==> Pouring git-2.18.0.high_sierra.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
zsh completions and functions have been installed to:
/usr/local/share/zsh/site-functions
Emacs Lisp files have been installed to:
/usr/local/share/emacs/site-lisp/git
==> Summary
/usr/local/Cellar/git/2.18.0: 1,488 files, 295.6MB
3.2 配置git
3.2.1 查看用户名和邮箱地址
fabric:~ fabric$ git config user.name
fabric:~ fabric$ git config user.email
3.2.2 修改用户名和邮箱地址
fabric:~ fabric$ git config --global user.name "你的用户名"
fabric:~ fabric$ git config --global user.email "你的邮箱地址"
3.2.3 生成SSH KEY
为了向github自己的仓库提交代码,我们需要设置SSH KEY
- 首先来生成SSH KEY
fabric:~ fabric$ ssh-keygen -t rsa -C 你的邮箱地址
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/fabric/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/fabric/.ssh/id_rsa.
Your public key has been saved in /Users/fabric/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:生成的密钥指纹 你的邮箱地址
The key's randomart image is:
+---[RSA 2048]----+
生成的密钥randomart image
+----[SHA256]-----+
- 复制上一步生成的公钥(public key),复制进github
fabric:~ fabric$ cat /Users/fabric/.ssh/id_rsa.pub
你的公钥
github设置公钥的地方:
右上角头像下拉选项 -> Settings -> SSH and GPG keys
- 可以试着在代码目录下拉取自己的仓库试试是否生效
cd 你的代码目录
git clone 你的代码git仓库地址
4 安装docker
Homebrew 的 Cask 已经支持 Docker for Mac,因此可以很方便的使用 Homebrew Cask 来进行安装:
4.1 用brew安装docker
fabric:~ fabric$ brew cask install docker
在载入 Docker app 后,点击 Next,可能会询问你的 macOS 登陆密码,你输入即可。之后会弹出一个 Docker 运行的提示窗口,状态栏上也有有个如下所示的小鲸鱼的图标:
验证一下是不是安装成功了呢,输入以下命令
fabric:~ fabric$ docker version
Client:
Version: 18.06.0-ce
API version: 1.38
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:05:26 2018
OS/Arch: darwin/amd64
Experimental: false
Server:
Engine:
Version: 18.06.0-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:13:46 2018
OS/Arch: linux/amd64
Experimental: true
4.2 下载app安装
如果需要手动下载,请点击以下链接下载 Stable 或 Edge 版本的 Docker for Mac。
如同 macOS 其它软件一样,安装也非常简单,双击下载的 .dmg 文件,然后将鲸鱼图标拖拽到 Application 文件夹即可。
4.3 给咱的docker提提速
由于一些大家都知道的原因,我们拉取Docker镜像会很缓慢,我们可以通过添加加速器来解决。比如网易的镜像地址:
http://hub-mirror.c.163.com
点击右上角的小鲸鱼图标 -> Perferences,然后选择下图中的Daemon->Registry mirrors,添加上面的地址
应用改动后,docker会重启,我们来看一下是否配置成功
fabric:~ fabric$ docker info
Containers: 0
Running: 0
...
Registry Mirrors:
http://hub-mirror.c.163.com/
Live Restore Enabled: false
可以看到,配置的地址已经生效了。
5 安装Goland
这里只是提供个工具推荐,安装Goland的文章网上一抓一大把,我就不赘述了。
如此一来,整个安装过程便完成了!
有疑问加站长微信联系(非本文作者)