Osx - `go get` fails with resolved failure in specific go packages

Installation of some go packages fails with permission denied error, for example:

 $ 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 to use sudo to fix the permission problem, it failed with the error $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 

How can i solve this?

+5
source share
2 answers
  • godoc , go tool vet , go tool cover , etc. are special go.tools , which should be installed by default into the system path using the go binary. If these commands are not available, try reinstalling it yourself (or find go.tools on your packaging system).

    Note. On OS X 10.8+, try installing using Homebrew instead of the official .pkg installer, and your problems should go away (like of go 1.4): brew install go

  • If you want to load a specific pkg into your $GOPATH (for example, a third-party dependent one), use go get -d <pkg> . Example:

     go get -d code.google.com/p/go.tools/cmd/cover 
  • You should not use the sudo hammer at all, as your $GOPATH should point to the directory that you have, and therefore the permission: denied error for everything.

    But if you really know what you are doing, and you still want sudo install something, you need to edit the sudoers file first to fix the GOPATH root:

     $ sudo visudo 

    add the following line:

     Defaults env_keep += "GOPATH" 

    This will cause sudo go get (the root context) to raise your $GOPATH .

+13
source

I just ran into this because I installed it using MacPorts. Vojtech Vitek's answer pointed me in the right direction, but I thought that I would go further and lay out the literal solution that I needed.

Run sudo port install go-tools . I do not know why Godoc et al. Are not included in the base go package, but whatever.

+2
source

Source: https://habr.com/ru/post/1206318/


All Articles