`go get`在某些Go包上出现权限被拒绝的错误。

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

`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

  1. godocgo tool vetgo tool cover等是特殊的go.tools命令,它们应该默认与go二进制文件一起安装到系统路径中。如果这些命令不可用,你应该尝试重新安装go本身(或在你的打包系统中找到go.tools)。

    注意:在OS X 10.8+上,尝试使用Homebrew安装go,而不是官方的.pkg安装程序,你的问题应该就解决了(从go 1.4开始):brew install go

  2. 如果你想将特定的包下载到你的$GOPATH中(例如第三方依赖),请使用go get -d <pkg>。例如:

    go get -d code.google.com/p/go.tools/cmd/cover
    
  3. 不应该需要使用sudo命令,因为你的$GOPATH应该指向你拥有的目录,因此不会出现permission: denied错误。

    但是,如果你确实知道自己在做什么,并且仍然想要使用sudo安装某些东西,你需要首先编辑sudoers文件以修复root的GOPATH:

    $ sudo visudo
    

    添加以下行:

    Defaults    env_keep += "GOPATH"
    

    这将使sudo go get(root上下文)使用你的$GOPATH值。

英文:
  1. 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 find go.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

  2. If you want to download a specific pkg into your $GOPATH (eg. 3rd party dependency), use go get -d &lt;pkg&gt; instead. Example:

     go get -d code.google.com/p/go.tools/cmd/cover
    
  3. You shouldn't need to use the sudo hammer at all, as your $GOPATH should point to a directory that you own, and thus no permission: 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 += &quot;GOPATH&quot;
    

    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.

huangapple
  • 本文由 发表于 2014年11月6日 14:39:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/26773198.html
匿名

发表评论

匿名网友

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

确定