The GOPATH
environment variable specifies the location of your workspace. If no GOPATH
is set, it is assumed to be $HOME/go
on Unix systems and %USERPROFILE%\go
on Windows. If you want to use a custom location as your workspace, you can set the GOPATH
environment variable.
This answer explains how to set this variable on various Unix systems.
GOPATH
can be any directory on your system. In Unix examples, we will set it to $HOME/go
(the default since Go 1.8). Note that GOPATH
must not be on the same path as your Go installation. Another common setup is to set GOPATH=$HOME
.
Go 1.13+
go env -w GOPATH=$HOME/go
Bash
Edit your ~/.bash_profile
to add the following line:
export GOPATH=$HOME/go
Save and exit your editor. Then, source your ~/.bash_profile
.
source ~/.bash_profile
Zsh
Edit your ~/.zshrc
file to add the following line:
export GOPATH=$HOME/go
Save and exit your editor. Then, source your ~/.zshrc
.
source ~/.zshrc
fish
set -x -U GOPATH $HOME/go
The -x
is used to specify that this variable should be exported
and the -U
makes this a universal variable, available to all sessions and
persistent.
PATH
variable to~/.zshrc
and not bash profile, the error shows you are usingzsh
and not bash – Festaecho $PATH
to see whether your PATH include/usr/local/go/bin
. If not, you've not edited the correct file, or not edited all the relevant files. – Orlov