Setting `GOPATH` for each vscode project

Setting the GOPATH global variable as an environment variable works fine with Visual Studio code.

But setting a variable for a project around the world seems to me not very pleasant. You have several Go projects, you have to change this variable every time you compile, debug, etc. Project.

Is it possible to set the GOPATH variable as a project variable in Visual Studio code? Ether in settings.json or launch.json ?

+5
source share
3 answers

Go 1.5 added a vendor directory that allows you to manage dependencies for each project.

If the source directory d / vendor exists, then when compiling the source file in the subtree, the root in d, import "p" is interpreted as importing "d / vendor / p" if this path names a directory containing at least one file with a name ending with to ".go".

a source

This feature is enabled by default with Go 1.6:

Go 1.5 introduced experimental support for the vendor directory, which was included with the environment variable. In Go 1.6, the feature is enabled by default.

a source

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

+2
source

In addition to the provider folder, you can still have one GOPATH for each project.

See " GOPATH from go.inferGopath :

GOPATH by go.inferGopath setting

Setting go.inferGopath overrides all of the above.
If go.inferGopath set to true , the extension will try to infer GOPATH from the workspace path, that is, the directory opened in vscode. It searches up the path for the src directory and sets GOPATH one level above this.

For example, if your project looks like /aaa/bbb/ccc/src/... , then opening the directory /aaa/bbb/ccc/src (or something below) will cause the extension to look up, find the component src on the way and set GOPATH one level up, i.e. GOPATH=/aaa/bbb/ccc .

This option is useful when you are working on different Go projects that have different GOPATH s. Instead of setting GOPATH in the workspace settings for each project or setting all paths as ;/: split lines, you can simply set go.inferGopath to true , and the extension automatically uses GOPATH .

GOPATH to install Go tools with go.toolsGopath

By default, all Go dependent tools are used from GOPATH derived from the above logic.
If available on your PATH , PATH used to search for Go tools.
If the Go tools are not in your way, you can get the same Go tools that are installed in each of your GOPATH s.
To prevent Go tools from cluttering up your GOPATH , use the go.toolsGopath parameter to provide a separate place for Go tools.

The first time you run go.toolsGopath, you will need to run the Go: Install Tools command so that the Go tools are installed in the provided location.

+3
source

GOPATH is your workspace and it is divided into

 GOPATH/ |- bin/ |- pkg/ |- src/ <--- your projects are saved here |- .../my_project1 |- .../my_project2 

With this separation, you do not need to install a new GOPATH for each project. I recommend you read How to write transition code

+2
source

Source: https://habr.com/ru/post/1260065/


All Articles