Mac os安裝golang開發(fā)環(huán)境

為了能夠愉快地進(jìn)行g(shù)olang編程,我們需要安裝以下幾樣?xùn)|西:

  • 包管理Homebrew
  • 語言環(huán)境golang
  • 版本管理git
  • 虛擬器docker
  • 編譯器Goland
    我將按照這個順序敘述整個安裝過程

docker 其實是可選的,它可以把應(yīng)用程序打包為可移植的、自給自足的容器。這樣一來,你就可以在本地生成golang程序的docker鏡像,直接扔到測試環(huán)境,便可以進(jìn)行測試了,不需要再進(jìn)行代碼上傳,環(huán)境配置等操作了。
如果你覺得暫時用不到,也可以先不裝。

1 安裝brew


HomeBrew圖標(biāo)

Homebrew有點(diǎn)類似于Linux操作系統(tǒng)中的apt-get(Ubuntu)、yum(yum),Mac的操作系統(tǒng)中使用它解決包依賴問題,套用官方的話來說:

Homebrew 能干什么?

使用 Homebrew 安裝 Apple 沒有預(yù)裝但 你需要的東西

讓我們開始安裝吧!

在命令行輸入以下指令

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


golang圖標(biāo)

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的相關(guān)環(huán)境變量
fabric:~ fabric$ vim ~/.bashrc

將下面內(nèi)容添加進(jìn)上面的文件

#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ù)個人習(xí)慣設(shè)置為其他目錄
本人習(xí)慣在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

這個在我另外一篇文章做了詳細(xì)的敘述,詳情請見《修改并編譯golang源碼》

3 安裝配置git


git圖標(biāo)

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自己的倉庫提交代碼,我們需要設(shè)置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]-----+
  • 復(fù)制上一步生成的公鑰(public key),復(fù)制進(jìn)github
fabric:~ fabric$ cat /Users/fabric/.ssh/id_rsa.pub 
你的公鑰

github設(shè)置公鑰的地方:

右上角頭像下拉選項 -> Settings -> SSH and GPG keys

設(shè)置公鑰
  • 可以試著在代碼目錄下拉取自己的倉庫試試是否生效
cd 你的代碼目錄
git clone 你的代碼git倉庫地址

4 安裝docker


docker

Homebrew 的 Cask 已經(jīng)支持 Docker for Mac,因此可以很方便的使用 Homebrew Cask 來進(jìn)行安裝:

4.1 用brew安裝docker

fabric:~ fabric$ brew cask install docker

在載入 Docker app 后,點(diǎn)擊 Next,可能會詢問你的 macOS 登陸密碼,你輸入即可。之后會彈出一個 Docker 運(yùn)行的提示窗口,狀態(tài)欄上也有有個如下所示的小鯨魚的圖標(biāo):


docker圖標(biāo)

驗證一下是不是安裝成功了呢,輸入以下命令

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安裝

如果需要手動下載,請點(diǎn)擊以下鏈接下載 StableEdge 版本的 Docker for Mac。

如同 macOS 其它軟件一樣,安裝也非常簡單,雙擊下載的 .dmg 文件,然后將鯨魚圖標(biāo)拖拽到 Application 文件夾即可。


拖拽安裝

4.3 給咱的docker提提速

由于一些大家都知道的原因,我們拉取Docker鏡像會很緩慢,我們可以通過添加加速器來解決。比如網(wǎng)易的鏡像地址:

http://hub-mirror.c.163.com

點(diǎn)擊右上角的小鯨魚圖標(biāo) -> Perferences,然后選擇下圖中的Daemon->Registry mirrors,添加上面的地址


增加倉庫

應(yīng)用改動后,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

這里只是提供個工具推薦,安裝Goland的文章網(wǎng)上一抓一大把,我就不贅述了。

如此一來,整個安裝過程便完成了!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容