Linux

為什麼“go version”命令失敗並出現錯誤?沒有這樣的文件或目錄

  • June 3, 2021

下面是Mac中的環境:

~$
~$
~$ echo $0
-bash
~$
~$
~$
~$
~$
~$ cat /etc/paths.d/go
/usr/local/go/bin
~$
~$ cat /etc/paths
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Users/MYUSER/go/bin
~$
~$
~$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/MYUSER/go/bin:/usr/local/go/bin
~$
~$ uname -a
Darwin XYXYXYXXYX 19.6.0 Darwin Kernel Version 19.6.0: Mon Apr 12 20:57:45 PDT 2021; root:xnu-6153.141.28.1~1/RELEASE_X86_64 x86_64
~$
~$ which go
/usr/local/go/bin/go
~$
~$
~$
~$ go version
-bash: /usr/local/bin/go: No such file or directory
~$
~$
~$
~$ /usr/local/go/bin/go version
go version go1.16.4 darwin/amd64
~$
~$

我 在安裝新版本rm -rf /usr/local/go之前執行rm /usr/local/bin/go了安裝go二進制的go v1.16/usr/local/go/bin

如何使go version命令工作?

在您在問題中發布的片段中,它說:

~$ which go
/usr/local/go/bin/go

which表示 go 二進製文件駐留在 /usr/local/go/bin 目錄中(另請參閱為什麼不使用“which”?那麼使用什麼?)但 Bash 嘗試從它執行 /usr/local/bin 目錄:

~$ go version
-bash: /usr/local/bin/go: No such file or directory

這可能是go二進製文件在您移動或刪除它之前所在的位置。在這種情況下,執行hash -r可能會解決問題,來自help hash

-r 忘記所有記住的位置

引用自:https://unix.stackexchange.com/questions/652676