英文:
`go get` fails with permission denied on certain go packages
问题
安装一些Go包时出现“权限被拒绝”的错误,例如:
$ go get code.google.com/p/go.tools/cmd/cover
go install code.google.com/p/go.tools/cmd/cover: open /usr/local/go/pkg/tool/darwin_amd64/cover: permission denied
当我尝试使用sudo
修复权限问题时,出现了$GOPATH not set
的错误:
$ sudo go get code.google.com/p/go.tools/cmd/cover
Password:
package code.google.com/p/go.tools/cmd/cover: cannot download, $GOPATH not set. For more details see: go help gopath
我该如何解决这个问题?
英文:
Installing some go packages fails with permission denied
error, eg.:
$ go get code.google.com/p/go.tools/cmd/cover
go install code.google.com/p/go.tools/cmd/cover: open /usr/local/go/pkg/tool/darwin_amd64/cover: permission denied
When I tried using sudo
to fix the permission issue, it failed with $GOPATH not set
error:
$ sudo go get code.google.com/p/go.tools/cmd/cover
Password:
package code.google.com/p/go.tools/cmd/cover: cannot download, $GOPATH not set. For more details see: go help gopath
How can I solve this?
答案1
得分: 14
-
godoc
、go tool vet
、go tool cover
等是特殊的go.tools命令,它们应该默认与go二进制文件一起安装到系统路径中。如果这些命令不可用,你应该尝试重新安装go本身(或在你的打包系统中找到go.tools
)。注意:在OS X 10.8+上,尝试使用Homebrew安装go,而不是官方的.pkg安装程序,你的问题应该就解决了(从go 1.4开始):
brew install go
-
如果你想将特定的包下载到你的
$GOPATH
中(例如第三方依赖),请使用go get -d <pkg>
。例如:go get -d code.google.com/p/go.tools/cmd/cover
-
你不应该需要使用
sudo
命令,因为你的$GOPATH
应该指向你拥有的目录,因此不会出现permission: denied
错误。但是,如果你确实知道自己在做什么,并且仍然想要使用
sudo
安装某些东西,你需要首先编辑sudoers文件以修复root的GOPATH:$ sudo visudo
添加以下行:
Defaults env_keep += "GOPATH"
这将使
sudo go get
(root上下文)使用你的$GOPATH
值。
英文:
-
The
godoc
,go tool vet
,go tool cover
etc. are special go.tools commands that are meant to be installed into the system path by default with the go binary. If these commands are not available, you should try reinstall go itself (or findgo.tools
in your packaging system).Note: On OS X 10.8+, try installing go using Homebrew instead of the official .pkg installer and your troubles should be gone (as of go 1.4):
brew install go
-
If you want to download a specific pkg into your
$GOPATH
(eg. 3rd party dependency), usego get -d <pkg>
instead. Example:go get -d code.google.com/p/go.tools/cmd/cover
-
You shouldn't need to use the
sudo
hammer at all, as your$GOPATH
should point to a directory that you own, and thus nopermission: denied
error at all.But if you really know what you're doing, and you still want to
sudo
install something, you need to edit the sudoers file first to fix the root's GOPATH:$ sudo visudo
add the following line:
Defaults env_keep += "GOPATH"
This will make the
sudo go get
(root context) pick up your$GOPATH
value.
答案2
得分: 2
我刚刚遇到了这个问题,因为我使用MacPorts安装了go。Vojtech Vitek的答案指引了我正确的方向,但我还是想提供我需要的确切解决方案。
运行sudo port install go-tools
。我不知道为什么godoc等工具没有包含在基本的go软件包中,但无论如何。
英文:
I just ran into this because I installed go with MacPorts. Vojtech Vitek's answer pointed me in the right direction but I thought I would go ahead and post the literal solution I needed.
Run sudo port install go-tools
. I don't know why godoc et al are not included with the base go package, but whatever.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论