无法在终端中运行Go二进制文件。

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

Can't Run Go Bin In Terminal

问题

我过去几周一直在Visual Studio的终端上执行我的Golang代码。突然间,我尝试运行我写的一个程序,但它无法在终端中运行。我输入go install project然后输入project。但是终端显示**-bash: project: command not found**。在VS中它显示project,但无法打开它。不过,如果我通过Finder并查看bin文件夹,我可以运行这个程序,这很奇怪。有没有什么建议可以解决这个问题?

英文:

I have been executing my Golang code on Visual Studio in the terminal for the past few weeks. All of a sudden I am trying to run a program I made, and it does not run in the terminal. I type go install project then type project. I get -bash: project: command not found. In VS it shows project, but is not able to open it. I can run the program if I go through the Finder and look in the bin though which is strange. Any suggestions how to correct this?

答案1

得分: 23

听起来你的$PATH环境变量没有设置为包含Go二进制文件。根据如何编写Go代码的说明:

为了方便起见,将工作空间的bin子目录添加到你的PATH中:

$ export PATH=$PATH:$(go env GOPATH)/bin

大多数人喜欢将这个命令添加到他们的.bashrc文件中,以便在会话之间保持持久化。

英文:

It sounds like your $PATH environment variable isn't set to include Go binaries. From How to Write Go Code:

> For convenience, add the workspace's bin subdirectory to your PATH:
>
> $ export PATH=$PATH:$(go env GOPATH)/bin

Most people like to add this to their .bashrc to persist it across sessions.

答案2

得分: 0

Windows用户将会有类似以下的设置 -

GOPATH="c:\Users\<username>\code\go"
PATH="...;%GOPATH%\bin"

你需要将这些添加到环境变量的路径中 -

环境变量

  1. 下载并安装Go发行版(GOROOT变量会自动设置)

  2. 在任意位置创建一个新文件夹作为你的工作空间,在其中创建3个目录:bin、src和pkg

  3. 然后打开

    控制面板 -> 所有控制面板项 -> 系统 -> 高级系统设置 -> 高级选项卡 -> 环境变量 ->
    通过在系统变量中点击“新建”添加新的系统变量 ->
    变量名 = GOPATH,变量值 =
    你创建的目录\路径

  4. 完成后,重新启动你的命令提示符或Bash(这很重要),你的GOPATH就设置好了。为了确认,运行go env命令,你将看到你的值。

英文:

Windows users will have something like -

GOPATH=&quot;c:\Users\&lt;username&gt;\code\go&quot;
PATH=&quot;...;%GOPATH%\bin&quot;

You will want to add this to path within environment variables -

Environment Variables

  1. Download and install Go distribution(the GOROOT variable was set
    automatically)

  2. Create new folder wherever you like for your workspace, create there
    3 directories: bin, src and pkg

  3. Then go to

    Control Panel -> All Control Panel Items -> System ->
    Advansed System Settings -> tab Advanced -> Environment Variables ->
    add new system variable by clicking New on System varaibles ->
    Variable name = GOPATH, Variable value =
    Your:\directory\that\you\created

  4. When you're done, RESTART your cmd or Bash(that's important) and you
    have your GOPATH set. To be sure run go env and you will see your
    value.

答案3

得分: 0

你也可以使用direnv,而不是添加到.bashrc

PATH_add $(go env GOPATH)/bin

这将在你的golang项目目录下工作时添加bin路径

英文:

you can also use direnv, instead of adding to .bashrc

PATH_add $(go env GOPATH)/bin

which adds the bin path when working under your golang project directory

huangapple
  • 本文由 发表于 2017年3月23日 09:33:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/42965673.html
匿名

发表评论

匿名网友

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

确定