英文:
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"
你需要将这些添加到环境变量的路径中 -
-
下载并安装Go发行版(GOROOT变量会自动设置)
-
在任意位置创建一个新文件夹作为你的工作空间,在其中创建3个目录:bin、src和pkg
-
然后打开
控制面板 -> 所有控制面板项 -> 系统 -> 高级系统设置 -> 高级选项卡 -> 环境变量 ->
通过在系统变量中点击“新建”添加新的系统变量 ->
变量名 = GOPATH,变量值 =
你创建的目录\路径 -
完成后,重新启动你的命令提示符或Bash(这很重要),你的GOPATH就设置好了。为了确认,运行go env命令,你将看到你的值。
英文:
Windows users will have something like -
GOPATH="c:\Users\<username>\code\go"
PATH="...;%GOPATH%\bin"
You will want to add this to path within environment variables -
-
Download and install Go distribution(the GOROOT variable was set
automatically) -
Create new folder wherever you like for your workspace, create there
3 directories: bin, src and pkg -
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 -
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论