I am trying to do a go get
:
go get github.com/go-sql-driver/mysql
and it fails with the following error:
packa
You have to update the PATH based on the terminal(bash or zsh) which you use.
~/.bashrc
or ~/.zshrc
in an editorvi ~/.zshrc (or) code ~/.zshrc
export GOPATH=$HOME/go:/$HOME/projects/go
Here you can add one or more paths separated by a semicolon :
from different locations of your GO projects on the system to the GOPATH
environment variable i.e /path/1:path/2:path/3
etc.
In my case, I have added 2 paths, as shown above, one from the root $HOME/go
and the other one from the projects directory :/$HOME/projects/go
New Way:
Check out this answer.
Old Way:
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
This is what got it working for me on Ubuntu 15.10 using the fish shell
:
# ~/.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
This script will help you switch Gopaths. https://github.com/buffonomics/goswitch
As written in the official instructions:
The GOPATH environment variable specifies the location of your workspace. It defaults to a directory named go inside your home directory, so $HOME/go on Unix, $home/go on Plan 9, and %USERPROFILE%\go (usually C:\Users\YourName\go) on Windows. If you would like to work in a different location, you will need to set GOPATH to the path to that directory. (Another common setup is to set GOPATH=$HOME.) Note that GOPATH must not be the same path as your Go installation.
So for example, if you are coding in Jetbrains Webstorm (using the Go plugin), you might want to set GOPATH
as /Users/<user>/WebstormProjects
.
In simpler words, set it to wherever you want your Go projects to reside.
Edit your ~/.bash_profile
to add the following line:
$ export GOPATH=$HOME/work
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/work/bin