我已经设置了我的$GOPATH,但它不起作用。

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

I have set my $GOPATH, but it doesn't work

问题

问题是无法下载github.com/golang/lint/golint包,因为$GOPATH未设置。根据错误信息,你可以尝试运行go help gopath命令获取更多详细信息。

你已经在~/.bash_profile文件中设置了$GOPATH,路径为$HOME/gocode

你可以运行go env命令来查看你的go环境变量设置。根据输出,GOPATH的值应该是/Users/wildcat/gocode

请检查你的环境变量设置是否正确,并确保$GOPATH已正确设置。

英文:
$ sudo go get -u github.com/golang/lint/golint
package github.com/golang/lint/golint: cannot download, $GOPATH not set. For more details see: go help gopath

I have set my $GOPATH:
(in ~/.bash_profile on my Mac)
export GOPATH=$HOME/gocode

And my go env:

$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/wildcat/gocode"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fno-common"
CXX="g++"
CGO_ENABLED="1"

What's the problem?

答案1

得分: 9

问题在于你正在使用 sudo:它将使用根环境变量而不是你的账户的环境变量。

你不应该使用 sudo,就像我在“如何在Mac OS X 10.10中设置GOPATH”中提到的那样:

  • sudo 有一个默认策略,会重置环境并设置安全路径
  • 除非你使用更复杂的 sudo -E bash -c 'go get github.com/golang/lint/golint' 命令:

目前,这应该足够了:

go get -u github.com/golang/lint/golint

OP 在评论中添加了一个不同的 go get 命令:

go install golang.org/x/tools/cmd/cover: 
  open /usr/local/go/pkg/tool/darwin_amd64/cover: permission denied 

这个命令将使用 $GOTOOLDIR(在你的情况下设置为“/usr/local/go/pkg/tool/darwin_amd64”)

正如在“go.tools的权限被拒绝错误”中所述,运行 sudo -s 然后执行 go get 命令应该可以解决问题。

英文:

The issue is that you are using sudo: it will use the root environment variable instead of the ones of your account.

You shouldn't need to use sudo, as I mentioned in "How to set GOPATH in Mac OS X 10.10":

  • sudo has a default policy of resetting the Environment and setting a secure path
  • unless you use the more complex sudo -E bash -c 'go get github.com/golang/lint/golint'):

For now, this should be enough:

go get -u github.com/golang/lint/golint

The OP adds a different go get command in the comments:

go install golang.org/x/tools/cmd/cover: 
  open /usr/local/go/pkg/tool/darwin_amd64/cover: permission denied 

That one would be using $GOTOOLDIR (set in your case to "/usr/local/go/pkg/tool/darwin_amd64")

As documented in "Permission denied error for 'go.tools'", running sudo -s then the go get command should work.

答案2

得分: 1

根据man页面的说明:
sudo允许授权用户以超级用户或其他用户的身份执行命令,具体取决于安全策略。

当使用sudo时,您是以root用户身份执行命令。我建议您删除sudo并尝试执行该命令。

英文:

sudo according to man:
>sudo allows a permitted user to execute a command as the superuser or
another user, as specified by the security policy.

When using sudo you are executing as root. I would suggest that you remove sudo and try executing it.

huangapple
  • 本文由 发表于 2015年3月4日 23:03:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/28857741.html
匿名

发表评论

匿名网友

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

确定