为每个VSCode项目设置`GOPATH`。

huangapple go评论81阅读模式
英文:

Setting `GOPATH` for each vscode project

问题

GOPATH变量设置为全局环境变量在Visual Studio Code中运行良好。

但是,我觉得在全局范围设置特定于项目的变量并不是很好。考虑到你有多个Go项目,每次编译、调试等都需要更改这个变量。

在Visual Studio Code中,是否有可能将GOPATH变量设置为项目变量?可以在settings.jsonlaunch.json中设置吗?

英文:

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?

答案1

得分: 17

(2018年第二季度:
请注意,使用vgo项目GOPATH可能会被弃用,而采用基于项目的工作流程。这将避免我在下面提出的手动基于项目的GOPATH,两年前的建议)

从Go 1.11开始(2018年8月),GOPATH可以是可选的,使用模块

在VSCode中越来越受支持:

此外,除了vendor文件夹,您仍然可以为每个项目设置一个GOPATH

参见"从go.inferGopath设置中获取GOPATH":

> ## 从go.inferGopath设置中获取GOPATH

> 设置go.inferGopath会覆盖上述所有内容。
如果将go.inferGopath设置为true,扩展将尝试从工作区路径(即在vscode中打开的目录)推断出GOPATH。它在路径中向上搜索src目录,并将GOPATH设置为该目录的上一级。

> 例如,如果您的项目路径为/aaa/bbb/ccc/src/...,那么打开目录/aaa/bbb/ccc/src(或其子目录)将导致扩展向上搜索,在路径中找到src组件,并将GOPATH设置为该组件的上一级,即GOPATH=/aaa/bbb/ccc

> 当您在不同的Go项目上工作时,此设置非常有用,这些项目具有不同的GOPATH。您可以将GOPATH设置为每个项目的工作区设置中,或者将所有路径设置为以;/:分隔的字符串,也可以将go.inferGopath设置为true,扩展会自动使用正确的GOPATH

> ## 使用go.toolsGopath安装Go工具的GOPATH

> 默认情况下,所有依赖的Go工具都使用从上述逻辑派生的GOPATH
如果它们在您的PATH中可用,则使用PATH来定位Go工具。
如果Go工具不在您的路径中,您可能会在每个GOPATH中安装相同的Go工具。
为了防止Go工具混乱您的GOPATH,请使用go.toolsGopath设置为Go工具提供单独的位置。

> 第一次设置go.toolsGopath时,您需要运行Go: Install Tools命令,以便将Go工具安装在提供的位置。

英文:

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

答案2

得分: 6

GOPATH是你的工作空间,它被分为以下几个部分:

GOPATH/
    |- bin/
    |- pkg/
    |- src/ <--- 你的项目保存在这里
        |- .../my_project1
        |- .../my_project2

通过这种分离,你不需要为每个项目设置一个新的GOPATH。我建议你阅读《如何编写Go代码》(How to Write Go Code)来了解更多信息。

英文:

The GOPATH is your workspace and it's divided in

GOPATH/
    |- bin/
    |- pkg/
    |- src/ &lt;--- 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

答案3

得分: 6

在Windows中设置工作区设置:

  1. 打开设置:ctrl+,

  2. 设置工作区设置:

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

使用分号 ; 分隔多个路径

  1. 重新启动Visual Studio Code以使设置生效。
英文:

set workspace settings, in windows:

  1. goto settings: ctrl+,

  2. set workspace setting:

     { 
          &quot;go.gopath&quot;: &quot;d:\\gopath;E:\\src&quot;
     }
    

use ; for multiple path

  1. restart visual studio code to take effect.

答案4

得分: 2

Go 1.5添加了供应商目录,允许每个项目进行依赖管理。

如果存在源目录d/vendor,在编译以d为根的子树中的源文件时,import "p"将被解释为import "d/vendor/p",如果该路径指定的目录包含至少一个以“.go”结尾的文件。

此功能在Go 1.6中已默认启用。

Go 1.5引入了对“vendor”目录的实验性支持,该支持通过环境变量启用。在Go 1.6中,该功能现在默认启用。

即使在1.6版本中,根据您使用的工具,您可能需要将GO15VENDOREXPERIMENT环境变量设置为1(在基于Unix的操作系统上使用export GO15VENDOREXPERIMENT=1)。

英文:

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)

答案5

得分: 2

如果你的VSCode项目目录是有组织的,

  1. 进入设置
  2. 要么搜索"Infer Gopath",要么在扩展/Go下找到这个条目
  3. 勾选该选项

然后,VSCode会从你的工作区根目录推断出GOPATH,这样就立即解决了我的问题。我相信这在其他答案中已经解释过了。我只是为了给出一个更简短的版本而发表这个回答。

英文:

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.

huangapple
  • 本文由 发表于 2016年11月21日 15:58:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/40715285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定