go config on mac

frontierdev

Jingtian Zhang

Posted on January 31, 2022

go config on mac

it is kinda painful to configure go and its tools on vscode on mac for the first time and second time (if you did not record what you did for the first time ...).

here are steps

  1. install golang using brew
brew install go

Enter fullscreen mode Exit fullscreen mode
  1. check the following commands
go env      // check go env vars
which go    // check which go
brew info go  // very useful info here

Enter fullscreen mode Exit fullscreen mode

till here everything should work fine, but I used oh-my-zsh (which is just based on zsh, so you just need to configure stuff on .zshrc file), so I configure my .zshrc file

cd 
nano ~/.zshrc
Enter fullscreen mode Exit fullscreen mode
#GOROOT
export GOROOT=/opt/homebrew/Cellar/go/1.17.5/libexec

#GOPATH WORKSPACE
export GOPATH=$HOME/go

#GOROOT BIN
export PATH=$PATH:$GOROOT/bin

#GOPATH BIN
export PATH=$PATH:$GOPATH/bin

#GOPROXY setup, this is qiniu proxy
export GOPROXY=https://goproxy.cn 
Enter fullscreen mode Exit fullscreen mode

you see that GOROOT info can be found from the previous step brew info go, and do not forget to add libexec after it, so it will become whatever I show above.

till here I decide to download a golang project and enter vscode to edit some go files, but I notice there are missing go-tools to install, and somehow they keep reporting errors

  1. timeout error (I am in China, so I need to configure a proxy for that)
  2. not found error (go tools not found issue explained below), can also check here

you can fix it by

mkdir -p $GOPATH/src/golang.org/x/

Enter fullscreen mode Exit fullscreen mode

and cd into this directory and :

git clone https://github.com/golang/tools.git

Enter fullscreen mode Exit fullscreen mode

after this go tools can be successfully implemented, and then you can go back to vscode and follow the popup and click install or install all to install go tools, and they should be good.

my final go root is the following:

GO111MODULE="off"
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/jingtianzhang/Library/Caches/go-build"
GOENV="/Users/jingtianzhang/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/jingtianzhang/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/jingtianzhang/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn"
GOROOT="/opt/homebrew/Cellar/go/1.17.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.17.5/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.17.5"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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 arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/42/m47sm8dn3rs4_ct90k_t9drm0000gn/T/go-build1902292704=/tmp/go-build -gno-record-gcc-switches -fno-common"
Enter fullscreen mode Exit fullscreen mode

here you can see GOPROXY, GOROOT, GOPATH, GO11MODULE setups here.

thank you very much and wish you good 2022 year

💖 💪 🙅 🚩
frontierdev
Jingtian Zhang

Posted on January 31, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related