Mac os安裝golang開發環境


為了能夠愉快地進行golang編程,我們需要安裝以下幾樣東西:

  • 包管理Homebrew
  • 語言環境golang
  • 版本管理git
  • 虛擬器docker
  • 編譯器Goland

我將按照這個順序敘述整個安裝過程

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

1、安裝brew


HomeBrew圖標

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


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


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


docker

Homebrew 的 Cask 已經支持 Docker for Mac,因此可以很方便的使用 Homebrew Cask 來進行安裝:

4.1 用brew安裝docker

fabric:~ fabric$ brew cask install docker

在載入 Docker app 后,點擊 Next,可能會詢問你的 macOS 登陸密碼,你輸入即可。之后會彈出一個 Docker 運行的提示窗口,狀態欄上也有有個如下所示的小鯨魚的圖標:
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

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

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM