Setting `GOPATH` for each vscode project
(Q2 2018:
Note that with the vgo 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.
It is more and more supported with VSCode:
- vscode-go/issue 1532
- "Go modules support in Visual Studio Code"
In addition to vendor folder, you still can have one GOPATH
per project.
See "GOPATH
from go.inferGopath
setting":
GOPATH
fromgo.inferGopath
settingSetting
go.inferGopath
overrides all of the above.
Ifgo.inferGopath
is set totrue
, the extension will try to infer theGOPATH
from the path of the workspace i.e. the directory opened in vscode. It searches upwards in the path for thesrc
directory, and setsGOPATH
to one level above that.For example, if your project looks like
/aaa/bbb/ccc/src/...
, then opening the directory/aaa/bbb/ccc/src
(or anything below that) will cause the extension to search upwards, find thesrc
component in the path, and set theGOPATH
to one level above that i.e.GOPATH=/aaa/bbb/ccc
.This setting is useful when you are working on different Go projects which have different
GOPATH
s. Instead of setting theGOPATH
in the workspace settings of each project or setting all the paths as;/:
separated string, you can just setgo.inferGopath
totrue
and the extension uses the rightGOPATH
automatically.
GOPATH
for installing the Go tools usinggo.toolsGopath
By default, all the dependent Go tools are used from the
GOPATH
derived from the above logic.
If they are available on yourPATH
, thePATH
is used to locate the Go tools.
If the Go tools are not in your path, you might end up with the same Go tools installed in each of yourGOPATH
s.
To prevent the Go tools from cluttering yourGOPATH
, use thego.toolsGopath
setting to provide a separate location for the Go tools.The first time you set go.toolsGopath, you will have to run
Go: Install Tools
command so that the Go tools get installed in the provided location.
The GOPATH is your workspace and it's divided in
GOPATH/
|- bin/
|- pkg/
|- src/ <--- your projects are saved here
|- .../my_project1
|- .../my_project2
With this separation, your don't need to set a new GOPATH for each project. I recommend you read How to Write Go Code
set workspace settings
, in windows:
- goto settings:
ctrl+,
set workspace setting:
{ "go.gopath": "d:\\gopath;E:\\src" }
use ;
for multiple path
- restart visual studio code to take effect.