GOPATH
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 page explains how to set this variable on various platforms.
GOPATH
can be any directory on your system. In Unix examples, we will set it to $HOME/go
(since Go 1.8, it's a default GOPATH
on Unix-like systems). Note that GOPATH
must not be the same path as your Go installation. Another common setup is to set GOPATH=$HOME
.
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
Note: Set the GOBIN path to generate a binary file when
go install
is run.export GOBIN=$HOME/go/bin
Edit your ~/.zshrc
file to add the following line:
export GOPATH=$HOME/go
Save and exit your editor. Then, source your ~/.zshrc
.
$ source ~/.zshrc
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
To set the GOBIN path:
set -x -U GOBIN $GOPATH/bin
Your workspace can be located wherever you like, but we'll use C:\go-work
in this example.
NOTE: that GOPATH
must not be the same path as your Go installation.
C:\go-work
.GOPATH
into the “Variable name” field.C:\go-work
into the “Variable value” field.There is a faster way to edit Environment Variables
with search
env
or environment
. select Edit environment variables for your account