The requirement to set GOPATH is a major issue for Go users who install Go tools for the first time and get the error: "you have to set a Republican Still need to add $GOPATH/bin to their PATH to run the executable initialized by go install to execute go get
Users developing in Go language still need to understand the existence, location and structure of GOPATH. If your GOROOT (where you check Go's source code) is the default GOPATH, the tool will refuse to break your GOROOT using the default GOPATH if no GOPATH is set. If the default doesn't suit you, you may still prefer to set a custom GOPATH.
GOPATH definitionGOPATH environment variable is used to specify the location of your workspace. If GOPATH is not set, it will be automatically set in a Unix-like system like this :
$HOME/go
%USERPROFILE%\go
Unix system
GOPATH can be set in any directory of your system Next. In the following example we will set it to $HOME/go (default after Go 1.8 version). Note that GOPATH cannot be the same as your go installation directory. The common setting is GOPATH=$HOME.
BashEdit ~/.bash_profile and add the following line:
export GOPATH=$HOME/go
Save and exit the editor, then execute
source ~/.bash_profile
Zsh
Edit your ~/.zshrc and add the following line:
export GOPATH=$HOME/go
Save and exit your editor. Then execute
source ~/.zshrc
fish
set -x -U GOPATH $HOME/go
-x is used to specify the variables you want to export -U is set to the global environment
Windows systemYour workspace can be defined into any directory you want to place it, below we use C:\go-work as an exampleNote: GOPATH cannot be the same as the Go installation directory.
Windows 7
Create directory C:\go-work.
Right-click on the computer icon and select Properties
Select Advanced System Settings on the left Select the bottom Environment variable
Select GOPATH in the upper window Click to edit the input value C:\go-work Save
Windows 10There is a faster one Way by searching for environment variables:
Left-click "Search" and type env or environment.
Select "Edit environment variables for your account". And follow the above windows7 steps.
View GOPATHgo env GOPATH
The above is the detailed content of How to set golang gopath. For more information, please follow other related articles on the PHP Chinese website!