英文:
go install always uses GOROOT/bin instead of GOPATH
问题
我在使用go install
命令时遇到了一个烦人的问题。
每次我尝试在我的GOPATH的src
目录中运行它时,结果文件总是在GOROOT/bin目录中创建,原因不明。
我在.bashrc中验证了我的环境变量,并运行了go env
命令(见下文),但没有找到任何问题:
.bashrc
export GOBIN=$HOME/dev/src/go/bin
export GOPATH=$HOME/dev/go-dev
export PATH=$PATH:$GOBIN:$GOPATH/bin
go env
GOARCH="amd64"
GOBIN="/home/user/dev/src/go/bin"
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/user/dev/go-dev"
GORACE=""
GOROOT="/home/user/dev/src/go"
GOTOOLDIR="/home/user/dev/src/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CGO_ENABLED="1"
这篇帖子有一个类似的问题,不过我在环境变量中有GOPATH(我尝试了解决方案,但没有帮助)。
当我尝试使用官方GoLang网站创建测试库并运行go install时,我在$GOPATH/pkg/linux_amd64目录中得到了一个正确的文件,但不在bin目录中。
我在配置中漏掉了什么吗?
英文:
I'm having annoying issue with go install
command.
Every time I try to run it within the src
directory of my GOPATH the resulted file is getting created in GOROOT/bin directory for some reason.
I verified my environmental variables in .bashrc and also run 'go env' (see below) and couldn't find any issues:
.bashrc
export GOBIN=$HOME/dev/src/go/bin
export GOPATH=$HOME/dev/go-dev
export PATH=$PATH:$GOBIN:$GOPATH/bin
go env
GOARCH="amd64"
GOBIN="/home/user/dev/src/go/bin"
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/user/dev/go-dev"
GORACE=""
GOROOT="/home/user/dev/src/go"
GOTOOLDIR="/home/user/dev/src/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CGO_ENABLED="1"
This post has a similar problem except I have GOPATH in my env (I tried the solution but it didn't help).
When I tried to create the test library using official GoLang site and run go install I've got a proper file created in $GOPATH/pgk/linux_amd64 but not in the bin directory.
Am I missing something in my configuration?
答案1
得分: 12
官方文档 关于 go
工具的说明:
> 如果 DIR 是 GOPATH 中列出的目录...
> 如果设置了 GOBIN 环境变量,命令将安装到该目录而不是 DIR/bin
关于这个主题在 邮件列表 上有讨论,其中进一步解释了这个问题:
> (a) 如果你没有设置 GOBIN 环境变量,
Go 编译器二进制文件将放在 GOROOT/bin 中,
而你的二进制文件将放在 GOPATH/bin 中。
(我个人喜欢这种二进制文件的分离。)
> (b) 如果你将 GOBIN 设置为任何值,
那么 Go 二进制文件和你的二进制文件都将放在 GOBIN 中。
在你的情况下,解决方案是不设置你的 GOBIN
。
英文:
Official documentation about the go
tool:
> If DIR is a directory listed in the GOPATH...
> If the GOBIN environment variable is set, commands are installed to the directory it names instead of DIR/bin
There has been a discussion about this topic on the mailing list where this is further explained:
> (a) If you don't set your GOBIN env variable,
you get the Go compiler binaries going in GOROOT/bin
whereas the your binaries are going in GOPATH/bin.
(I personally like this separation of binaries.)
> (b) If you set your GOBIN to anything, then
both the Go binaries and your binaries are going to GOBIN.
The solution in your case would be to not set your GOBIN
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论