gpt4 book ai didi

go - 为每个 vscode 项目设置 `GOPATH`

转载 作者:IT老高 更新时间:2023-10-28 13:09:53 29 4
gpt4 key购买 nike

GOPATH 变量全局设置为环境变量适用于 Visual Studio Code。

但在全局范围内设置项目特定变量对我来说似乎不是很好。假设您有多个 Go 项目,每次编译、调试等项目时都必须更改此变量。

是否可以在 Visual Studio Code 中将 GOPATH 变量设置为项目变量? settings.jsonlaunch.json 中的以太币?

最佳答案

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

使用 Go 1.11(2018 年 8 月),GOPATH can be optional, with modules .

VSCode 越来越支持它:


除了v​​endor文件夹,每个项目还可以有一个GOPATH

参见“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.

关于go - 为每个 vscode 项目设置 `GOPATH`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40715285/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com