当 process.env[“GOROOT”] 被设置时,不支持切换 Go 版本。

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

Switching Go version when process.env["GOROOT"] is set is unsupported

问题

我正在使用M2,并使用brew安装了Go 1.20。现在我想在VSCode中切换Go版本:

但是它失败了,并显示以下警告信息:

当设置了process.env["GOROOT"]时,不支持切换Go版本。

我从.zshrc中删除了GOROOT后,env中的GOROOT消失了。但它仍然存在于go env中。我该如何从go env中删除GOROOT?我还尝试从${HOME}/Library/Application Support/go/env中删除它,但我仍然无法切换Go版本。

➜  ~ env |grep GOROOT
➜  ~ go env GOROOT
/usr/local/go
英文:

I am using M2, and use brew installed go 1.20. Now I want to switch Go version in VSCode:

当 process.env[“GOROOT”] 被设置时,不支持切换 Go 版本。

But it failed with this warning message:

Switching Go version when process.env["GOROOT"] is set is unsupported.

当 process.env[“GOROOT”] 被设置时,不支持切换 Go 版本。

After removing GOROOT from .zshrc, GOROOT has been disapeared from env. But it still exist in go env. How can I remove GOROOT from go env? I also tried to remove it from ${HOME}/Library/Application Support/go/env, but I still can not switch Go version.

➜  ~ env |grep GOROOT
➜  ~ go env GOROOT
/usr/local/go

答案1

得分: 1

选择 Go 环境是 vscode-go 扩展的一个功能(参见管理 Go 版本)。

如果在 .zshrc 文件中移除了 GOROOT,并且运行 env | grep GOROOT 的输出为空,则说明你已经正确地使该功能生效。

请注意,这个更改不会立即在 VSCode 中生效,需要重新启动 VSCode。有个棘手的问题是,如果 VSCode 是由设置了 GOROOT 的进程启动的,那么 VSCode 仍然会看到它。所以最好重新启动系统以确保安全。


> 现在我必须在 settings.json 中设置 "go.goroot" 来更改 Go 版本。

在重新启动 VSCode 或重新启动系统后,应该删除这个配置。否则,你会收到以下警告:

Switching Go version when "go.goroot" is set is unsupported.

> 但是它仍然存在于 go env 中。我该如何从 go env 中删除 GOROOT

这是正常的。你不需要也不能从 go env 的输出中删除它。

go env GOROOT 显示的是 GOROOT 的有效设置。它显示的值来自以下来源(优先级从高到低):

  1. 当前 go 命令推断的路径。例如,如果你安装了 go1.19,并使用 go install golang.org/dl/go1.19@latest 进行安装,运行 go1.19 env GOROOT 将会给出类似 $HOME/sdk/go1.19 的路径。
  2. GOROOT 环境变量;
  3. Go 环境配置文件中设置的值(go env GOENV 显示配置文件的路径)。
  4. PATH 的部分中找到的 go 命令推断的路径(与上面的第 1 点相比,这有点令人困惑)。
英文:

Choose Go Environment is a feature of the vscode-go extension (see Managing Your Go Version).

If after removing GOROOT from .zshrc, the output from env | grep GOROOT is empty, then you have done the right thing to make the feature work.

Bear in mind that the change won't be observed by VSCode immediately. It's required to restart VSCode. The tricky thing is, if VSCode is started by a process that has GOROOT set, VSCode will still see it. So maybe it's better to restart the system to be safe.


> Now I have to set "go.goroot" in settings.json to change the go version.

You should remove this configuration after restart VSCode or reboot the system. Otherwise, you will get this warning:

Switching Go version when "go.goroot" is set is unsupported.

> But it still exist in go env. How can I remove GOROOT from go env.

This is expected. You don't need to, and can't, remove it from the output of go env.

go env GOROOT shows the effective setting of GOROOT. It shows the value from one of the following sources (higher priority first):

  1. the path inferred from the current go command. For example, if you have go1.19 installed with go install golang.org/dl/go1.19@latest, running go1.19 env GOROOT will give you the path like $HOME/sdk/go1.19.
  2. the GOROOT environment variable;
  3. the value set in the Go environment configuration file (go env GOENV shows the path to the configuration file).
  4. the path inferred from the go command found in the PATH parts (this is a little confusing compared to point 1 above).

huangapple
  • 本文由 发表于 2023年5月22日 21:27:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306692.html
匿名

发表评论

匿名网友

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

确定