-
Notifications
You must be signed in to change notification settings - Fork 762
gopath
The GOPATH
environment variable is a fundamental part of writing Go code
without Go modules. It specifies the location of your workspace, and it
defaults to $HOME/go
. A GOPATH
directory contains src
, bin
, and pkg
directories. Your code is typically located in the $GOPATH/src
directory.
If you are not familiar with Go and GOPATH
, please first read about writing
Go code with GOPATH
.
If you are just starting out with Go, we recommend using Go modules instead of GOPATH.
- Check the value of
GOPATH
- Setting
GOPATH
- Different
GOPATH
s for different projects - Automatically inferring your
GOPATH
- Install tools to a separate
GOBIN
- Install tools to a separate
GOPATH
First, it's useful to quickly check that you are using the right GOPATH
. Two commands report the GOPATH
value used by the VS Code Go extension: (1) Go: Current GOPATH
, or (2) Go: Locate Configured Go Tools
. Use either of these commands to check which GOPATH
the extension is using.
If the GOPATH
value is incorrect, see the details below on how to configure it.
If you have chosen not to use Go modules, you will need to configure your GOPATH
.
Setting GOPATH
is typically as simple as setting the environment variable once in your system's configuration. Take a look at the Setting GOPATH
Wiki if you're unsure how to do this.
By default, the extension uses the value of the environment variable GOPATH
. If no such environment variable is set, the extension runs go env
and uses the GOPATH
reported by the go
command.
Note that, much like a PATH
variable, GOPATH
can contain multiple directory paths, separated by :
or ;
. This allows you to set different GOPATH
s for different projects.
Still, there are a number of cases in which you might want a more complicated GOPATH
set-up. Below, we explain more complex ways to configure and manage your GOPATH
within the VS Code Go extension.
Setting go.gopath
in your user settings overrides the environment's GOPATH
value.
Workspace settings override user settings, so you can use the go.gopath
setting to set different GOPATH
s for different projects. A GOPATH
can also contain multiple directories, so this setting is not necessary to achieve this behavior.
NOTE: This feature only works in GOPATH
mode, not in module mode.
The go.inferGopath
setting overrides the go.gopath
setting. If you set go.inferGopath
to true
, the extension will try to infer your GOPATH
based on the workspace opened in VS Code. This is done by searching for a src
directory in your workspace. The parent of this src
directory is then added to your global GOPATH
(go env GOPATH
).
For example, say your global GOPATH
is $HOME/go
, but you are working in a repository with the following structure.
foo/
└── bar
└── src
└── main.go
If you open the foo
directory as your workspace root in VS Code, "go.inferGopath"
will set your GOPATH
to $HOME/go:/path/to/foo/bar
.
This setting is useful because it allows you to avoid setting the GOPATH
in the workspace settings of each of your projects.
If you switch frequently between GOPATH
s, you may find that the extension prompts you to install tools for every GOPATH
. You can resolve this by making sure your tool installations are on your PATH
, or you can configure a separate directory for tool installation: GOBIN
. This environment variable tells the go
command where to install all binaries. Configure it by setting:
"go.toolsEnvVars": {
"GOBIN": "path/to/gobin"
}
NOTE: The following is only relevant if you are using a Go version that does not support Go modules, that is, any version of Go before 1.11.
Before Go 1.11, the go get
command installed tools and their source code to your GOPATH
. Because this extension uses a lot of different tools, this causes clutter in your GOPATH
. If you wish to reduce this clutter, you can have the extension install tools to a different location. This also addresses the issue described above, when switching GOPATHs
forces you to reinstall Go tools.
This can be done by setting "go.toolsGopath"
to an alternate path, only for tool installations. After you configure this setting, be sure to run the Go: Install/Update Tools
command so that the Go tools get installed to the provided location.
The extension will fall back to your existing GOPATH
if tools are not found in the go.toolsGopath
directory.
✏️ Want to contribute to this wiki?
Update the source and send a PR.