golang: go install 尝试将文件安装到 /usr/local 而不是 GOPATH。

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

golang: go install tries /usr/local instead of GOPATH

问题

这是对我上一个问题的一种后续:https://stackoverflow.com/questions/12514037/golang-installing-packages-in-a-local-directory

我将GOPATH设置为$HOME/prog/go/gopath,并且该路径下存在三个目录:

~/prog/go/gopath$ ls
bin  pkg  src

现在我尝试安装一个用于访问redis数据库的模块,它要求我在源代码目录中运行

go install

但是go install命令给出了以下结果:

~/prog/go/gopath/src/redis (go1)$ go install
go install flag: open /usr/local/go/pkg/darwin_amd64/flag.a: permission denied
~/prog/go/gopath/src/redis (go1)$ echo $GOPATH 
<myhomedir>/prog/go/gopath

(其中<myhomedir>是一个有效的路径)

问题1:为什么go install没有考虑到$GOPATH
问题2:如何说服go install使用$GOPATH

英文:

This is somewhat a follow-up to my last question: https://stackoverflow.com/questions/12514037/golang-installing-packages-in-a-local-directory

I have GOPATH set to $HOME/prog/go/gopath and this path exists with three directories:

~/prog/go/gopath$ ls
bin  pkg  src

Now I try to install a module to access the redis database which asks me to run

go install

inside the source directory. But the command go install gives me

~/prog/go/gopath/src/redis (go1)$ go install
go install flag: open /usr/local/go/pkg/darwin_amd64/flag.a: permission denied
~/prog/go/gopath/src/redis (go1)$ echo $GOPATH 
<myhomedir>/prog/go/gopath

(where <myhomedir> is a valid path)

Question 1: why does go install not take $GOPATH into account?
Question 2: how to convince go install to use $GOPATH?

答案1

得分: 6

不确定你如何设置go,但有可能需要从标准库构建包,但由于权限问题无法进行。你可以尝试以下操作:

cd /usr/local/go/src
sudo ./all.bash

这将构建标准库并运行测试以确保一切正常。

确保你有适当的权限来读取和执行$GOROOT中所需的内容。个人建议只需从golang.org下载存档,并将其保存在~/local/go下,并相应地设置GOROOT。

英文:

Not sure how you setup go but it's possible that it needs to build packages from std library but can't due to permissions. You can try

cd /usr/local/go/src
sudo ./all.bash

This should build the std library and run tests to make sure everything is ok.

Make sure you have proper permissions to read and execute from $GOROOT as necessary. Personally I just download the archive from golang.org and keep it under ~/local/go and set GOROOT appropriately.

答案2

得分: 5

类似的问题在这里。当我检查我的$GOROOT时,我发现所有的库已经在那里构建好了。但是由于某些原因,它尝试重新构建所有的库。所以我只是做了一个小技巧:

find /usr/lib/go/pkg/ -name "*.*" | sudo xargs touch

然后一切都正常工作了。

英文:

Similar problems here. When I check my $GOROOT, I find that all the libraries are already built there. But for some reasons, it tries to rebuild all the libraries. So I just do a little trick:

find /usr/lib/go/pkg/ -name "*.*" | sudo xargs touch

Then everything just work fine.

答案3

得分: 3

我认为你需要的命令是"go get":

go get github.com/alphazero/Go-Redis

将会下载Go-Redis库并将其放入你的$GOPATH/src目录中。

go install会对你自己的源代码进行编译和安装。

我必须承认,我对这个概念有些困惑,但是仔细阅读"How to Write Go"和代码组织部分包含了关于go命令如何工作的所需知识。

英文:

I think the command you need is "go get":

go get github.com/alphazero/Go-Redis

will download the Go-Redis library and put it into your $GOPATH/src directory.

go install performs a compile and install on your own source code.

I must admit, I struggled with this whole concept for a bit, but a careful re-reading of "How to Write Go" and the code organization section contains what you need to know about how the go command works.

答案4

得分: 0

解决方案是从您的.bash_profile文件中删除GOROOT。然后,go命令将安装到您的GOPATH目录中。
而奇怪的是:当我再次在我的.bash_profile中设置GOROOT并创建一个新的shell时,它也起作用。

英文:

The solution is remove GOROOT from your .bash_profile. Then the go command will install it to your GOPATH directory.
And so strange is: when I set the GOROOT in my .bash_profile again and create a new shell, it also works.

huangapple
  • 本文由 发表于 2012年9月21日 02:09:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/12518374.html
匿名

发表评论

匿名网友

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

确定