為了能夠愉快地進行golang編程塌忽,我們需要安裝以下幾樣東西:
- 包管理Homebrew
- 語言環(huán)境golang
- 版本管理git
- 虛擬器docker
- 編譯器Goland
我將按照這個順序敘述整個安裝過程
docker 其實是可選的捧搞,它可以把應用程序打包為可移植的、自給自足的容器段磨。這樣一來籽前,你就可以在本地生成golang程序的docker鏡像诱鞠,直接扔到測試環(huán)境闹瞧,便可以進行測試了,不需要再進行代碼上傳唧取,環(huán)境配置等操作了铅鲤。
如果你覺得暫時用不到,也可以先不裝枫弟。
1 安裝brew
Homebrew有點類似于Linux操作系統(tǒng)中的apt-get(Ubuntu)邢享、yum(yum),Mac的操作系統(tǒng)中使用它解決包依賴問題淡诗,套用官方的話來說:
Homebrew 能干什么?
使用 Homebrew 安裝 Apple 沒有預裝但 你需要的東西骇塘。
讓我們開始安裝吧!
在命令行輸入以下指令
fabric:~ fabric$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
出現(xiàn)以下內(nèi)容韩容,安裝成功
==> 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
我們發(fā)現(xiàn)最新的有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的相關環(huán)境變量
fabric:~ fabric$ vim ~/.bashrc
將下面內(nèi)容添加進上面的文件
#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可以根據(jù)個人習慣設置為其他目錄
本人習慣在home目錄下的Documents里新建一個code目錄款违,用于存放各種語言的代碼,比如Documents/code/gopath用于存放golang的代碼群凶,Documents/code/www用于存放php代碼...
讓改動生效
fabric:~ fabric$ source ~/.bashrc
- 試一試golang是否安裝成功
出現(xiàn)以下內(nèi)容插爹,則安裝成功
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 已經(jīng)支持 Docker for Mac,因此可以很方便的使用 Homebrew Cask 來進行安裝:
4.1 用brew安裝docker
fabric:~ fabric$ brew cask install docker
在載入 Docker app 后毅弧,點擊 Next气嫁,可能會詢問你的 macOS 登陸密碼,你輸入即可够坐。之后會彈出一個 Docker 運行的提示窗口寸宵,狀態(tài)欄上也有有個如下所示的小鯨魚的圖標:
驗證一下是不是安裝成功了呢,輸入以下命令
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鏡像會很緩慢聋迎,我們可以通過添加加速器來解決。比如網(wǎng)易的鏡像地址:
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
可以看到奶卓,配置的地址已經(jīng)生效了。
5 安裝Goland
這里只是提供個工具推薦,安裝Goland的文章網(wǎng)上一抓一大把,我就不贅述了。