For every project I create, I have to do export GOPATH={path_to_project}
every time I cd into the project dir. There has to be an easier way. Isn't there some way I can create a .bashrc or .bash_profile file for a given directory to define the GOPATH for that project?
For example, I have two go projects A and B. If I have a singular GOPATH that isn't redefined when I move between projects, then binaries for both projects will be stored in the same place. More importantly, binaries for third party libraries will be stored in the same place, so I have no way of maintaining multiple versions of the same library on a per project basis.
However, if I am able to define GOPATH on a per project basis, then all binaries and third party libraries are project dependent. This seems to be the common way of handling package management in most other language environments (ruby rbenv, python vertiualenv, etc.)
GOPATH is a variable that defines the root of your workspace. By default, the workspace directory is a directory that is named go within your user home directory (~/go for Linux and MacOS, %USERPROFILE%/go for Windows). GOPATH stores your code base and all the files that are necessary for your development.
The Go programmer isn't supposed to set GOPATH manually when entering a new project folder. Each project folder is supposed to be a package by itself, and reside in the GOPATH along other packages, so GOPATH should be set only once.
The go command identifies the appropriate GOROOT automatically based on its own directory location. GOPATH defaults to $HOME/go . You only need to set it explicitly if you want to put it somewhere else. This location can be overridden using the GOBIN environment variable.
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.
(Q2 2018: Note that with the vgo (now "module") project, GOPATH
might end up being deprecated in favor of a project-based workflow. That would avoid the manual project-based GOPATH
I was proposing below, two years ago)
With Go 1.11 (August 2018), GOPATH
can be optional, with modules.
You have a similar idea expressed in Manage multiple GOPATH dirs with ease, by Herbert Fischer (hgfischer
), for a Linux/Unix environment (base on the question already mention in the comments above):
Just include the following snippet in your
~/.bashrc
(or~/.bash_profile
) and reload your shell environment withsource ~/.bashrc
.
This snippet will create a shell function that will override the builtin commandcd
with a customized one that scans the entered directory, and every other above, for a file named.gopath
.
cd () { builtin cd "$@" cdir=$PWD while [ "$cdir" != "/" ]; do if [ -e "$cdir/.gopath" ]; then export GOPATH=$cdir break fi cdir=$(dirname "$cdir") done }
Now you just need to create a
.gopath
file in every directory you want as yourGOPATH
and every time you enter this directory, the redefinedcd
function will set theGOPATH
of your current environment to this directory.
Update 2017: if you don't want to modify your environment, you can still use one GOPATH
per project, by opening the src
folder of that project in Visual Studio Code (vscode, which is a multi-platform IDE), combined with the extension "Go for Visual Studio Code".
In that IDE, you can:
GOPATH
in a setting called go.toolsGopath
.GOPATH
with a setting called go.inferGopath
That way, VSCode will install a collection of tools in your global GOPATH
(for you to use outside VSCode as well).
See "Go tools that the Go extension depends on": godep
, golint
, guru
, godoc
, ...
And yet, your GOPATH
for your project will be the parent folder of src:
That works when you compile/install your project from the IDE.
If you want to do it from the command line, the original answer above would still apply.
You can use a tool like autoenv to set up a script that is automatically executed when you cd
into a particular directory.
For your purposes, an example /happy/go/path/yay/.env
file might look like:
export GOPATH="/happy/go/path/yay" export PATH="$GOPATH/bin:$PATH"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With