安装go-eval后出现“command not found”错误。

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

Error "command not found" after installing go-eval

问题

我正在尝试以交互模式运行Go。我想使用go-eval来实现,我按照他们的README说明进行了操作:

  • 我成功运行了go get github.com/sbinet/go-eval/
  • 我运行了go-eval,结果是-bash: go-eval: command not found

一些更多的信息:

  • echo $PATH返回:/usr/local/go/bin:...

  • echo $GOPATH返回:$HOME/golang

  • 运行whereis go-eval没有输出

  • 运行go install go-eval返回:

can't load package: package go-eval: cannot find package "go-eval" in any of:
      /usr/local/go/src/go-eval (from $GOROOT)
      $HOME/golang/src/go-eval (from $GOPATH)
英文:

I'm trying to run Go in an interactive mode.

I want to use go-eval for that, I followed their README instructions:

  • I ran go get github.com/sbinet/go-eval/ successfully
  • I ran go-eval which resulted in -bash: go-eval: command not found

Some more information:

  • echo $PATH returns: /usr/local/go/bin:...

  • echo $GOPATH returns: $HOME/golang

  • running whereis go-eval returns no output

  • running go install go-eval returns:

can't load package: package go-eval: cannot find package "go-eval" in any of:
/usr/local/go/src/go-eval (from $GOROOT)
$HOME/golang/src/go-eval (from $GOPATH)

答案1

得分: 130

你需要将GOPATH/bin添加到PATH中。

PATH="$GOPATH/bin:$PATH"

更新 [Go 1.8及以上版本]GOPATH将默认为$HOME/go。如果未显式设置GOPATH,上述方法将不起作用

要同时设置两者,请将以下内容添加到你的.profile文件中:

export GOPATH="$HOME/go"
PATH="$GOPATH/bin:$PATH"
英文:

You'll need to add GOPATH/bin to PATH.

PATH="$GOPATH/bin:$PATH"

Update [Go 1.8 and above]: GOPATH will default to $HOME/go. The above will not work if GOPATH is not explicitly set.

To set both, add this to your .profile file:

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

答案2

得分: 7

将以下内容添加到~/.zshrc文件中:

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

然后运行source ~/.zshrc命令。

英文:
  1. add those line in to ~/.zshrc

    export GOPATH="$HOME/go"
    export PATH=$PATH:$GOPATH/bin
    
  2. run source ~/.zshrc

答案3

得分: 6

二进制文件go-eval是否在$GOPATH/bin目录下?你是否在以$GOPATH/bin/作为工作目录运行命令?如果不是,那可能是问题所在。

go getgo install会将Go二进制文件(如果有的话)安装在$GOPATH/bin目录下。

检查$GOPATH/bin目录是否存在go-eval二进制文件。如果存在,尝试在$GOPATH/bin目录下使用./go-eval运行它。如果可以正常运行,那就没问题了。

如果你希望以后可以在任何位置运行$GOPATH/bin目录下的Go二进制文件,请将以下内容添加到你的.bashrcprofile文件中:

export PATH=$PATH:$GOPATH/bin

然后重新启动终端或运行. ~/.bashrc. /etc/profile命令。

当运行go install go-eval时,我得到以下错误:

无法加载包:包go-eval不存在于以下任何目录中:
/usr/local/go/src/go-eval(来自$GOROOT)
$HOME/golang/src/go-eval(来自$GOPATH)

你得到上述错误是因为go-eval不在$HOME/golang/src/go-eval目录中。运行go get github.com/sbinet/go-eval/将会将源代码下载到$GOPATH/src/github/sbinet/go-eval/目录下。如果你想运行go install go-eval命令,你需要根据其在目录层次结构中的位置指定相应的包名,即$GOPATH/src中的位置。

例如:
go install github/sbinet/go-eval

英文:

Is the binary go-eval in $GOPATH/bin? Are you running the command with $GOPATH/bin/ as your working directory? If not, thats likely the problem.

go get & go install installs go binaries (if any) in $GOPATH/bin

Check $GOPATH/bin for the go-eval binary. If its there, try running it from $GOPATH/bin with ./go-eval. If that works, you're good.

In future, if you wish to run go binaries found in $GOPATH/bin from anywhere in your shell, add the following to your .bashrc or profile:

export PATH=$PATH:$GOPATH/bin

Then restart your terminal or run . ~/.bashrc or . /etc/profile

> When running go install go-eval I get:

>can't load package: package go-eval: cannot find package "go-eval" in any of:
> /usr/local/go/src/go-eval (from $GOROOT)
> $HOME/golang/src/go-eval (from $GOPATH)

You get the above error because go-eval is not in $HOME/golang/src/go-eval. Running go get github.com/sbinet/go-eval/ will download the source to $GOPATH/src/github/sbinet/go-eval/. If you want to run go install go-eval, you have to specify the package name relevant to its position in the directory hierarchy in $GOPATH/src.

e.g.
go install github/sbinet/go-eval

答案4

得分: 5

在使用export PATH="~/go/bin:$PATH"时遇到了这个问题。

似乎~符号引起了问题,改为使用完整路径可以解决。

尝试使用以下方式,不使用波浪线符号:

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

英文:

Ran into this issue while using export PATH="~/go/bin:$PATH".

Seems the ~ was causing problems and changing to the full path worked.

Try something like this instead, which won't use a tilde:

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

答案5

得分: 2

我想要补充一下之前的回答。

作为一个有用的提示,你可以使用which命令来测试你的命令。

例如:which go

如果命令找不到,那么你就知道你有一个需要解决的路径问题。

然后你可以使用find命令来查找该命令。

例如:find / -name "go" -print 2>/dev/null

第一个斜杠是搜索的起始目录,-name后面的参数是你要查找的命令,-print用于显示好的结果。2>/dev/null将不可访问的目录的结果发送到空(null),这样你就不需要看到一堆错误信息。

使用这个过程可以帮助你快速找到你要找的命令,然后你可以将它添加到你的PATH环境变量中,这样就可以像其他答案中所述那样访问它了。

英文:

I'd like to add this in addition to the answers given.

As a helpful tip, uou can always test your commands with the which command.

Such as: which go

If the command is not found, you know you have a PATH problem you need to address first.

Then you can focus on finding the command with the find command.

Such as: find / -name "go" -print 2>/dev/null

The first slash is the directory to start the search, the argument to the -name is the command you're looking for and the -print gets a good results look. the 2>/dev/null sends results of directories that are not accessible to neverland (null) so that you do not need to see a bunch of errors.

Using this process helps you quickly find the command in question and then you can add it to your PATH env variable and it becomes accessible as stated in the other answers.

答案6

得分: 2

以上所有的答案都是自解释的。除此之外,我想补充一点,默认情况下,只有将bin文件夹添加到环境变量中的命令才能在终端中直接访问,无论是在Linux、Mac还是Windows上。

否则,您将需要指定该软件的bin文件夹路径,后跟二进制文件的名称。例如,在您的情况下,是<path_to_bin_folder>/go-eval

这是您无法直接从命令行执行该命令的最常见原因。请记住这一点,并在在网上寻找答案之前尝试这个方法,因为这很可能会解决您的问题。您只需要知道安装路径。

因此,请将以下内容写入终端的rcprofile文件中并保存,例如对于zsh,它是~/.zshrc,对于bash,它是~/.bash_profile~/.bash_rc

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

现在,尽管文件已保存,但更改不会立即生效。您需要像上面提到的那样,对配置文件进行资源化。为此,请键入source ~/.zshrc。现在您应该能够直接从命令行运行该命令了。即使问题仍然存在,请尝试退出终端会话,然后重新登录。

如果您想为其他软件包添加bin文件夹的路径,可以使用:将其附加到$PATH环境变量中。例如,如果您还想添加Java二进制文件的路径,只需执行以下操作:

export PATH=$PATH:$JAVA_HOME/bin

此外,将一个软件包的根文件夹路径定义为单独的环境变量(例如$GOPATH="$HOME/go")是一个好习惯。如果将来安装路径发生更改,您只需更新与该二进制文件相关的环境变量(例如,只需更新$GOPATH="newpath"),您的命令将像以前一样工作,因为$GOPATH的更改将反映在$PATH中。

英文:

All above answers are self explaining. Over and above those I would like to add that by default you can access only those commands from terminal without path to binary whose bin folder is added to the environment variable, be it linux, mac or windows.

Else you will have to specify the path to bin folder of that software followed by the binary name. For instance in your case &lt;path_to_bin_folder&gt;/go-eval.

This is the most common reason that you are not able to execute that command directly from the command line. Please remember this and try this before searching for answers online because this will most probably solve your issue. All you have to know is the installation path.

So, write the following into the rc or profile file for your terminal and save, example for zsh it is ~/.zshrc, for bash it is ~/.bash_profile or ~/.bash_rc.

export GOPATH=&quot;$HOME/go&quot;
export PATH=$PATH:$GOPATH/bin

Now although the file is saved but the changes will not reflect immediately. You have to source the profile file as mentioned above. For this type source ~/.zshrc. You should now be able to run the command directly from command line now. Even if the problem still persists try quit the terminal session and logging off and then logging in.

In case you want to add path to bin folder for other packages, you can append it to the $PATH environment variable using :. So for example if you want to add path to java binary as well, then just do

export PATH=$PATH:$JAVA_HOME/bin

Also it is a good practice to define the path to root folder of a package in its separate environment variable(example $GOPATH=&quot;$HOME/go&quot;). In case if the installation path changes in future then you'll just have to update the environment variable related to that binary (example just update, $GOPATH="newpath") and your command will work as previously, since the change in $GOPATH will reflect in $PATH.

huangapple
  • 本文由 发表于 2016年3月18日 19:46:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/36083542.html
匿名

发表评论

匿名网友

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

确定