go downloaded with apt,but go command not found

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

go downloaded with apt,but go command not found

问题

你的操作系统是Kali。首先,我输入go,Shell告诉我找不到go命令。

然后我执行了sudo apt install golang-go,它告诉我golang-go已经是最新版本,但是go仍然找不到。我通过谷歌搜索得知有时会有一个/usr/local/go文件夹,但是我没有找到这个文件夹。

接着我执行了locate go,找到了/usr/lib/go,然后进入/usr/lib/go/bin目录。

/usr/lib/go/bin目录下,我执行了./go version./go env命令,成功输出了Go的版本信息和环境变量。

虽然命令可以在/usr/lib/go/bin目录下执行,但为什么go命令仍然找不到呢?

/usr/lib/go/bin/usr/local/go目录有什么区别?如果它们没有区别,我该如何使用go而不是cd /usr/lib/go/bin && ./go呢?

英文:

my os is Kali.First i enter go,shell told me go command not found

└─$ go       
Command 'go' not found, but can be installed with:
sudo apt install gccgo-go 
sudo apt install golang-go

so i sudo apt install golang-go and it told me golang-go is already latest version,but go was still not found.And i google to knows sometime it will have a floder /usr/local/go but it not have one.

┌──(highway㉿highway)-[/usr/local]
└─$ ll       
drwxr-xr-x 2 root root 4096  5月 16 13:27 bin
drwxr-xr-x 2 root root 4096  2月  8 01:26 etc
drwxr-xr-x 2 root root 4096  2月  8 01:26 games
drwxr-xr-x 2 root root 4096  2月  8 01:26 include
drwxr-xr-x 5 root root 4096  4月 23 17:28 lib
lrwxrwxrwx 1 root root    9  4月 23 17:28 man -> share/man
drwxr-xr-x 3 root root 4096  4月 24 08:09 samba
drwxr-xr-x 2 root root 4096  2月  8 01:26 sbin
drwxr-xr-x 7 root root 4096  4月 24 08:09 share
drwxr-xr-x 2 root root 4096  2月  8 01:26 src

so i locate go and find /usr/lib/go and cd /usr/lib/go/bin

┌──(highway㉿highway)-[/usr/lib/go]
└─$ ls             
api  bin  doc  misc  pkg  src  test  VERSION

┌──(highway㉿highway)-[/usr/lib/go/bin]
└─$ ll
-rwxr-xr-x 1 root root 10225816  5月 15 03:22 go
-rwxr-xr-x 1 root root  2281848  5月 15 03:22 gofmt

┌──(highway㉿highway)-[/usr/lib/go/bin]
└─$ ./go version  
go version go1.18.2 linux/amd64

┌──(highway㉿highway)-[/usr/lib/go/bin]
└─$ ./go env     
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/highway/.cache/go-build"
GOENV="/home/highway/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/highway/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/highway/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.18"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.18/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3356979286=/tmp/go-build -gno-record-gcc-switches"

it work,but why go is still not found?

┌──(highway㉿highway)-[/usr/lib/go/bin]
└─$ go   
Command 'go' not found, but can be installed with:
sudo apt install gccgo-go 
sudo apt install golang-go

what different between floder /usr/lib/go/bin and /usr/local/go?if there were not different what can i do to use go instead of cd /usr/lib/go/bin && ./go.

答案1

得分: 1

你需要将go的bin目录添加到你的PATH环境变量中。在~/.bashrc文件的末尾添加以下行:

export PATH=$PATH:/usr/lib/go/bin

英文:

You need to append go's bin directory to your PATH environment variable. Add this line at the end of ~/.bashrc:

export PATH=$PATH:/usr/lib/go/bin

答案2

得分: 1

替代@emilianolch解决方案的方法是在/usr/local/bin中创建符号链接。您可能希望将/usr/lib/go/bin中的所有二进制文件链接起来,所以可以执行以下操作:

sudo ln -s /usr/lib/go/bin/* /usr/lib/bin/

这样就可以立即生效,无需重新启动终端。

优点:Go二进制文件与其他发行版一样显示。可能与其他工具(如VS Code SSH Remote)更好地配合使用。

缺点:如果将来通过apk安装更多的Go工具,则需要再次执行ln操作。(仅适用于新安装的工具,如果重新安装/升级现有工具,则不需要)

英文:

An alternative to @emilianolch solution is to create symlinks in /usr/local/bin. And you properly want all the binaries in /usr/lib/go/bin linked, so:

sudo ln -s /usr/lib/go/bin/* /usr/lib/bin/

Should work right away without restarting terminal.

PRO: Go binaries appear as they are like other distro. May work better with other tools like VS Code SSH Remote.

CON: Have to ln again if more Go tools are installed via apk in future. (Only new ones, not needed if reinstalling/upgrading existing ones)

huangapple
  • 本文由 发表于 2022年5月28日 08:05:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/72412041.html
匿名

发表评论

匿名网友

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

确定