Member-only story
Golang Tips
The Only Correct Way To Configure GoLang Development Environment for VSCode On MacOS
GOPATH And GOROOT Correct Set Up In Mac And In VSCode
Many developers on iOS have encountered the problems of installing Golang and configure the environment variables, such as
build esc: cannot load xxx : malformed module path “xxx”: missing dot in first path element
warning: GOPATH set to GOROOT (/Users/xxx/go/) has no effect
In fact, they are all caused by the misconfiguration of the two key parameters of GOPATH and GOROOT.
Precondition: installing GoLang on Mac OS with brew, rather than use pkg distribution
brew install go
The correct values for GOPATH and GOROOT
edit ~/.bash_profile by adding the following lines :
source ~/.bash_profile
Here is the reason behind:
GOPATH
is the place where we get, build and install packages outside the standard Go tree (so it’s NOT where the executables are!), so it is customizable and thus you can set GOPATH wherever you want.
However, GOROOT
is the place where the Go binary distributions assume they will be installed (so in Linux distributions, it’s normally in /usr/local/go
, but for MacOS, the Go and the tools are installed in /usr/local/Cellar/go/1.13/x.xx.xx by default as well as a soft link to /usr/local/opt/go/libexec will be created at the same time), so on Linux normally we don’t have to set this variable, but on MacOS, we have to change it as brew installs Go tools to a different path.
Config vscode settings
if you also use vscode as your Golang editor and want to debug in a friendly GUI pattern, install the go extension as the following:

Now it’s the key part, in vscode, open the settings.json in the directory: ~/Library/Application…