由于需要進行go語言項目的開發,基于自己個人的Mac電腦做了一些開發環境的配置,本文旨在做一個簡單的記錄。
先介紹下本文的電腦配置:
image.png
一、安裝brew
Homebrew有點類似于Linux操作系統中的apt-get(Ubuntu)、yum(yum),Mac的操作系統中使用它解決包依賴問題,套用官方的話來說:
讓我們開始安裝吧!
在命令行輸入以下指令
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
二、安裝并配置golang
首先看看有哪些golang版本可用
louxiujun@IT-C02Z9057LVDL /usr/local/opt brew search go
==> Formulae
algol68g google-java-format
anycable-go google-sparsehash
arangodb google-sql-tool
argon2 googler
aws-google-auth goolabs
bogofilter goose
cargo-completion gopass
certigo gor
cgoban goreleaser
clingo goreman
django-completion gost
forego gosu
fuego gotags
gnu-go goto
go gource
go-bindata govc
go-jira govendor
go-md2man gowsdl
go-statik gox
go@1.10 ? gst-plugins-good
go@1.11 gx-go
go@1.12 hugo
go@1.9 jfrog-cli-go
goaccess jpegoptim
goad lego
gobby lgogdownloader
gobject-introspection libgosu
gobuster mongo-c-driver
gocr mongo-cxx-driver
gocryptfs mongo-orchestration
godep mongoose
goenv pango
gofabric8 pangomm
goffice protoc-gen-go
golang-migrate pygobject
gollum pygobject3
golo ringojs
gom spaceinvaders-go
gomplate spigot
goocanvas svgo
goofys wego
google-authenticator-libpam wireguard-go
google-benchmark write-good
==> Casks
homebrew/cask-fonts/font-go
homebrew/cask-fonts/font-go-medium
homebrew/cask-fonts/font-go-mono
homebrew/cask-fonts/font-go-mono-nerd-font
homebrew/cask-fonts/font-go-mono-nerd-font-mono
homebrew/cask/go-agent
homebrew/cask/go-server
選擇一個自己想要的版本,這里本人選擇的是go@1.10版本。
安裝brew下go@1.10
版本:
louxiujun@IT-C02Z9057LVDL $ brew install go@1.10
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.10/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.10/1.9.7: 7,668 files, 294.2MB
確認一下go@1.10
的安裝目錄:
louxiujun@IT-C02Z9057LVDL $ cd /usr/local/opt/
louxiujun@IT-C02Z9057LVDL $ ls
gettext go@1.10 libidn2 libunistring openssl@1.1 wget
可以看到二進制的go@1.10
已經被安裝在該目錄下了。
配置golang的相關環境變量
louxiujun@IT-C02Z9057LVDL $ vim ~/.bashrc
將下面內容添加進上面的文件
#GOROOT
export GOROOT=/usr/local/opt/go\@1.10
#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/java
用于存放java代碼等。
讓改動立即生效:
louxiujun@IT-C02Z9057LVDL $ source ~/.bashrc
試一試golang是否安裝成功
出現以下內容,則安裝成功
louxiujun@IT-C02Z9057LVDL /usr/local/opt go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/louxiujun/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/louxiujun/Documents/code/gopath"
GORACE=""
GOROOT="/usr/local/opt/go@1.10"
GOTMPDIR=""
GOTOOLDIR="/usr/local/opt/go@1.10/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
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"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/2h/43trw35142g1h7qs8zdgxbn40000gp/T/go-build985605356=/tmp/go-build -gno-record-gcc-switches -fno-common"