user387049
user387049

Reputation: 6877

Installing Go packages in non-default directories

I've created an API in Go in which I've installed two packages via go get and have created four others for my own use. Everything seems to work fine. However I would like to move where the packages are so that I can have everything within my Git directory.

From what I've read, it should be as easy as changing my GOPATH variable to the new directory, and running go get again to test. Is this correct? If it is, am I doing anything wrong? Below is what shows in the terminal (Mac OS X) when I run go env. If any other info would be useful, please let me know. Thanks.

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Applications/MAMP/htdocs/git/cbi/api"
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CGO_ENABLED="1"

I should clarify that by default my GOPATH was an empty string. I tried updating it to the directory above since that is where I wish new packages be installed, and also for Go to look for any packages I've created an placed there.

I'm setting my GOPATH variable by running:

export GOPATH="/Applications/MAMP/htdocs/git/cbi/api"

After doing this, I can see the variable has been set when I type go env. However if I quit Terminal and then open it back up, and run go env again, the GOPATH variable is not set. Is there a reason it's not saving? Should I be setting it another way instead of export?

Upvotes: 1

Views: 5056

Answers (3)

Rick-777
Rick-777

Reputation: 10258

My approach has been to start a new Go project by putting the following into env.sh in my project dir

export GOPATH=$PWD
export PATH=$GOPATH/bin:$PATH

Then I always 'source' this file before I start work in a new terminal window, i.e.

. env.sh

Then when I use 'go get', it puts all the downloaded source code into the current project folder and I can commit it to my chosen version control system. This is the advantage of this approach: everything is held in one directory tree which can easily be versioned.

If I should happen to forget to source env.sh, Go commands usually fail immediately because I don't have write access to /usr/local/go files, which is the default place for them to be put. So I soon found I didn't forget.

Upvotes: 2

Jeff Li
Jeff Li

Reputation: 1120

If you don't set the GOPATH variable, the packages will be installed in the GOROOT which is not recommended. If you want to set GOPATH to "/Applications/MAMP/htdocs/git/cbi/api", make sure the directory of your code is put in "/Applications/MAMP/htdocs/git/cbi/api/src/".

However if I quit Terminal and then open it back up, and run "go env" again, the GOPATH variable is not set.

Like jeremy says, you should set the environment variable in bash configure file. In mac os, add this to your $HOME/.profile : export GOPATH=/Applications/MAMP/htdocs/git/cbi/api . BTW, you can specify multiple GOPATH, for exmple, export GOPATH=/path/to/gopath1:/path/to/gopath2

Upvotes: 3

Joe
Joe

Reputation: 6610

Please make sure GOPATH have three subfolder:src,pkg/linux_amd64(this one has to do with the OS),bin

More details can refer: GOPATH and Build

Upvotes: 1

Related Questions