15

Setting the GOPATH variable global as an enviroment variable works fine with Visual Studio Code.

But setting a project specific variable globally doesn't seem very nice to me. Consider you have multiple Go projects, you would have to change this variable each time you, compile, debug, ... etc. a project.

Is there a possibility to set the GOPATH variable as a project variable in Visual Studio Code? Ether in settings.json or launch.json?

Matthias
  • 1,055
  • 2
  • 14
  • 26
  • don't try to change `GOPATH` for each project, [use `vendor` folder](https://blog.gopheracademy.com/advent-2015/vendor-folder/) – n00dl3 Nov 21 '16 at 08:56

5 Answers5

17

(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:


In addition to vendor folder, you still can have one GOPATH per project.

See "GOPATH from go.inferGopath setting":

GOPATH from go.inferGopath setting

Setting go.inferGopath overrides all of the above.
If go.inferGopath is set to true, the extension will try to infer the GOPATH from the path of the workspace i.e. the directory opened in vscode. It searches upwards in the path for the src directory, and sets GOPATH 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 the src component in the path, and set the GOPATH 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 GOPATHs. Instead of setting the GOPATH in the workspace settings of each project or setting all the paths as ;/: separated string, you can just set go.inferGopath to true and the extension uses the right GOPATH automatically.

GOPATH for installing the Go tools using go.toolsGopath

By default, all the dependent Go tools are used from the GOPATH derived from the above logic.
If they are available on your PATH, the PATH 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 your GOPATHs.
To prevent the Go tools from cluttering your GOPATH, use the go.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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • `inferGopath` with `toolsGopath` is great. Thx – merito Oct 11 '18 at 03:15
  • @merito Indeed, but note that all that is obolete with Go modules now (1.11+): https://github.com/golang/go/wiki/Modules. I have edited the answer accordingly. – VonC Oct 11 '18 at 07:19
6

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

Motakjuq
  • 2,199
  • 1
  • 11
  • 23
  • 4
    This is not very sufficient when you have a solution with different projects of different languages. – Matthias Nov 21 '16 at 09:06
  • What do you mean with different languages? GOPATH is only your workspace for yours golang projects. If you're using other languages, they're not affected by GOPATH env variable. If you're taking about versioning, the vendor directory is your solution. – Motakjuq Nov 21 '16 at 09:22
  • 4
    Consider you are writing applications of many microservices each and in each one of them is written in `Go` the others in `C++`, `JavaScript`, whatever... . Then your workspace tree looks strange... – Matthias Nov 21 '16 at 12:28
  • helpful answer. this is kinda dumb design tho, it assumes you want all your `go` code in only one directory: `$GOPATH/src`, but what if I want more than one `$GOPATH/src` folder on my machine? – Alexander Mills Oct 19 '18 at 05:18
6

set workspace settings, in windows:

  1. goto settings: ctrl+,
  2. set workspace setting:

    { 
         "go.gopath": "d:\\gopath;E:\\src"
    }
    

use ; for multiple path

  1. restart visual studio code to take effect.
hustljian
  • 965
  • 12
  • 9
  • 1
    This is correct to setup the default GOPATH for VS Code. As in my case I had a different GOPATH while VSCODE was still pointing to the old GOPATH. Thus, I added this config and `“go.toolsGopath”: “/Users/user/go/”` to tell VS Code to install tools in correct GOPATH. Thanks for sharing – bmalhi Aug 13 '20 at 02:31
2

Go 1.5 added the vendor directory that allows a per-project dependency management.

If there is a source directory d/vendor, then, when compiling a source file within the subtree rooted at d, import "p" is interpreted as import "d/vendor/p" if that path names a directory containing at least one file with a name ending in “.go”.

source

This feature has been enabled by default with Go 1.6:

Go 1.5 introduced experimental support for a “vendor” directory that was enabled by an environment variable. In Go 1.6, the feature is now enabled by default.

source

Even with the 1.6 version, depending on the tools you use, you might need to set the GO15VENDOREXPERIMENT environment variable to 1 (export GO15VENDOREXPERIMENT=1 on unix-based OS)

n00dl3
  • 21,213
  • 7
  • 66
  • 76
2

If your VSCode project directory is organized,

  1. go to Settings
  2. either search "Infer Gopath" or find this entry under Extensions/Go
  3. check the box

Then VSCode infers GOPATH from your workspace root, which solves my problem immediately. I believe this is explained in other answers. I am posting this just to give a shorter version.

Louis
  • 766
  • 7
  • 10