【问题标题】:The term 'go' is not recognized as the name of a cmdlet, function, script file, or operable program术语“go”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称
【发布时间】:2018-02-03 21:39:11
【问题描述】:

运行后...

go run main.go

我收到此错误

go : The term 'go' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.

【问题讨论】:

  • 如果您解决了自己的问题,请将解决方案放在答案中,而不是问题中。

标签: go visual-studio-code


【解决方案1】:

已解决:转到 Visual Studio 代码(假设已安装) ctrl + shift + p 打开命令窗口类型

Go:当前的 GOPATH

这应该为您提供一个路径,创建一个名为 GOPATH 的系统环境变量并将该路径添加到该变量中。

确保系统环境变量设置为:

GOPATH 设置(在我的情况下)为 C:\Users[username]\go

GOROOT 设置为 C:\go\

IDE 是 Visual Studio Code。

【讨论】:

  • 在 Windows 灌输中,Go MSI 将 GOPATH 安装为用户变量而不是系统变量。感谢您的修复。
  • 我明白了 - Go: : 术语“Go:”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。
  • 需要重启VSCode。
【解决方案2】:
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")

将代码写入终端并运行它。解决问题的简单方法。

【讨论】:

  • 这只能工作一次,不能永久有效!
【解决方案3】:

我有同样的问题。然后在 O'REILLY 的《Learning Go》一书中找到了下面的解决方案。

如果您在使用 bash 的类 Unix 系统上,请将以下行添加到您的 .profile 中。 (如果您使用的是 zsh,请将这些行添加到 .zshrc):

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

您需要获取 $HOME/.profile 以使这些更改在您的当前生效 租用终端窗口。 在 Windows 上,在命令提示符处运行以下命令:

setx GOPATH %USERPROFILE%\go
setx path "%path%;%USERPROFILE%\bin"

运行这些命令后,您必须关闭当前的命令提示符并 打开一个新的以使这些更改生效。

【讨论】:

    【解决方案4】:

    对于通过 pip 安装的命令,我遇到了类似的问题,我必须将 Scripts 文件夹的完整路径添加到 PATH 变量中

    C:\Users\username\.pyenv\pyenv-win\versions\3.x.y\Scripts
    

    对于 go too,我相信也有类似的事情要做。

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 2021-11-01
      • 2021-05-23
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多