Can I have multiple GOPATH directories?
I set my GOPATH to
/Users/me/dev/go
and I have
/Users/me/dev/go/src/client1
/Users/me/dev/go/src/client2
/Users/me/dev/go/src/client3
and also
/Users/me/dev/client1/rails_project
/Users/me/dev/client2/php_project
etc.
I don't like how in my root dev folder I'm forced to have this general "go" dir that holds many different client's go projects.
Solution 1:
Yes, GOPATH
is a list of directories (like PATH
). Run go help gopath
for details. For example, on Linux, I have:
$ go env
GOROOT="/home/peter/go"
GOPATH="/home/peter/gopath:/home/peter/public/gopath"
$
I have something similar on Windows.
Note: Linux uses :
as the GOPATH
list separator; Windows uses ;
as the separator.
If you use go get
it will default to the first directory in the list.
Run go env
to check that everything is correct.
Solution 2:
This blog post gives a very nice explanation about how and why one would set multiple values in GOPATH, especially when it says:
My GOPATH consists of 3 folders or GOPATH workspaces.
The first one is my landing workspace. Since it's listed first, whenever I go get any new package, it always ends up in this workspace.
Go searches each directory listed in GOPATH to find source code, but new packages are always downloaded into the first directory in the list. I make it a rule to never do any development in there, so it's always completely safe to clean this folder whenever it gets too large (with Go packages I don't use). After all, it only has Go packages that I can get again with go get.
My second workspace is for all my personal Go packages and any other packages I may want to "favorite" or do some development on. I move things I use regularly from first workspace into second.
My third workspace is dedicated to the private Go packages from my work, and their dependencies. It's convenient to have my work packages separate from all my personal stuff, so they don't get in each other's way.
Solution 3:
Yes.
To cite Go itself:
$ go help gopath
The Go path is used to resolve import statements. It is implemented by and documented in the go/build package.
The GOPATH environment variable lists places to look for Go code.
On Unix, the value is a colon-separated string.
On Windows, the value is a semicolon-separated string.
On Plan 9, the value is a list.GOPATH must be set to get, build and install packages outside the standard Go tree.
Each directory listed in GOPATH must have a prescribed structure:
The src directory holds source code. The path below src determines the import path or executable name.
...