- 如何在 Ubuntu 上设置 GOPATH 环境变量？ 我必须编辑什么文件？
I am trying to do a
go get github.com/go-sql-driver/mysql
and it fails with the following error:
package github.com/go-sql-driver/mysql: cannot download, $GOPATH not set. For more details see: go help gopath
when I do a
go env, a list of Go values is shown as below:
ubuntu@ip-xxx-x-xx-x:~$ go env GOARCH="amd64" GOBIN="" GOCHAR="6" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="" GORACE="" GOROOT="/usr/lib/go" GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64" CC="gcc" GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread" CGO_ENABLED="1"
clearly the GOPATH is not set, how and where do I set it?
I see many threads that mention this error but none that provide an answer to my question, which file needs to be edited to provide a value for this path?
Just add the following lines to ~/.bashrc and this will persist. However, you can use other paths you like as GOPATH instead of $HOME/go in my sample.
export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
My go environment looked similar to yours.
$go env GOARCH="amd64" GOBIN="" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="" GORACE="" GOROOT="/usr/lib/go-1.6" GOTOOLDIR="/usr/lib/go-1.6/pkg/tool/linux_amd64" GO15VENDOREXPERIMENT="1" CC="gcc" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0" CXX="g++" CGO_ENABLED="1"
I resolved it with setting GOPATH to /usr/lib/go. Try it out.
export GOPATH=/usr/lib/go export PATH=$PATH:$GOPATH/bin
Additionally you can add this string to file
There is no need to edit any file, we can just use the command above to directly edit the Go environment variables.
This is what got it working for me on Ubuntu 15.10 using the
# ~/.config/fish/config.fish set -g -x PATH /usr/local/bin $PATH set -g -x GOPATH /usr/share/go
Then I had to change the permissions on the
go folder (it was set to root)
sudo chown <name>:<name> -R /usr/share/go
Shortest and current solution.
Write this code in Terminal.
Note: This will reset on every new Terminal window or system restart.
To be persistent, paste the code below in your
.bashrc file according to your shell. Those files in your Home Directory. It will be like below.
export PATH=path/to/some/other/place/composer/for/example export GOPATH=path/to/your/gopath/directory export PATH=$PATH:$GOPATH/bin
If for example, it is an Ubuntu, after installing the packages:
$sudo apt install golang -y
Just add the following lines to ~/.bashrc (Of your user)
export GOROOT=/usr/lib/go export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
If you've setup anything that needs modification of environment variables e.g. Java, Go etc this will be very familiar.
I will assume that you have the following directory structure somewhere as your Go path:
\---[folder name] +---bin +---pkg \---src
- open a new terminal
sudo nano /etc/environment
PATH=...and go the end of it and add a colon
:after the last path then paste in your full
and you're done, This should make it persistent through the system.
go path could be every where you want just create a directory and set global path variable in the name of GOPATH to your environment.
mkdir ~/go export GOPATH=~/go go get github.com/go-sql-driver/mysql
GOPATH is an environment variable to your work-space location. GOROOT is an environment variable to your installation directory. Although GOROOT and GOPATH is automatically set (if there would not be a bug) during the installation time, to specify it manually you can follow below process. Moreover, for more information you can refer to GO's wiki page.
It is better to set GOPATH to a directory inside your home directory, e.g., $HOME/go, %USERPROFILE%\go (Windows).
- This is a solution mac, which is tested on macOS Sierra, ver. 10.12, and also in Gogland-EAP, which has been introduced as an IDE for Go programming by JetBrains.
On your Terminal type
in opened document on the Terminal press i and add the following path
GOPATH=/path/to/a/directory/inside/home/directory GOROOT=/path/to/you/go/library PATH=$PATH:$GOPATH:$GOROOT:$GOROOT/bin
press ESC and type :x. Lastly, you should restart (close and open) your terminal or Logout and Login again.
- For Windows and Linux configuration, please refer to Go wiki page at Githab on Setting GOPATH-Golang.
CAUTION Do not set both GOROOT and GOPATH to the same directory, otherwise you will get a warning.