安裝工具
首先,先安裝Homebrew:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
檢視brew是否安裝成功:
brew -v
使用brew安裝go:
brew install go
檢視go是否安裝成功:
go version
配置環境變數
如果.bash_profile不存在,則執行vi語句:
vi ~/.bash_profile
在 .bash_profile 中加入如下(下面1.16.3為go的版本號):
export GOPATH=/usr/local/Cellar/go/1.16.3
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
輸入如下程式碼,完成對golang環境變數的配置,配置成功沒有提示:
source ~/.bash_profile
檢視配置結果:
go env
macdeimac:go mac$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/mac/Library/Caches/go-build"
GOENV="/Users/mac/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/mac/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/mac/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/Cellar/go/1.16.3"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.16.3/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.3"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/h1/k9c4q6053jl8jkst6hyyp07h0000gn/T/go-build3049851687=/tmp/go-build -gno-record-gcc-switches -fno-common"
本作品採用《CC 協議》,轉載必須註明作者和本文連結