英文:
Setting `GOPATH` for each vscode project
问题
将GOPATH
变量设置为全局环境变量在Visual Studio Code中运行良好。
但是,我觉得在全局范围设置特定于项目的变量并不是很好。考虑到你有多个Go
项目,每次编译、调试等都需要更改这个变量。
在Visual Studio Code中,是否有可能将GOPATH
变量设置为项目变量?可以在settings.json
或launch.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中越来越受支持:
- vscode-go/issue 1532
- "在Visual Studio Code中支持Go模块"
此外,除了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 GOPATH
s. 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 GOPATH
s.
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/ <--- 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中设置工作区设置:
-
打开设置:
ctrl+,
-
设置工作区设置:
{ "go.gopath": "d:\\gopath;E:\\src" }
使用分号 ;
分隔多个路径
- 重新启动Visual Studio 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.
答案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”.
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.
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项目目录是有组织的,
- 进入设置
- 要么搜索"Infer Gopath",要么在扩展/Go下找到这个条目
- 勾选该选项
然后,VSCode会从你的工作区根目录推断出GOPATH,这样就立即解决了我的问题。我相信这在其他答案中已经解释过了。我只是为了给出一个更简短的版本而发表这个回答。
英文:
If your VSCode project directory is organized,
- go to Settings
- either search "Infer Gopath" or find this entry under Extensions/Go
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论