繁体   English   中英

使用 go 构建时如何修复“未设置 GOPATH”错误

[英]How to fix 'GOPATH not set' error when using go build

我正在尝试构建一些 go 项目。 从项目目录运行go build返回以下错误:

../../../github.com/getsentry/sentry-go/integrations.go:4:2: cannot find package "bufio" in any of:
    /home/user/go/src/bufio (from $GOROOT)
    ($GOPATH not set. For more details see: 'go help gopath')
vendor/github.com/getsentry/raven-go/client.go:5:2: cannot find package "bytes" in any of:
    /home/user/go/src/gitlab.com/company/bc_to_influx/vendor/bytes (vendor tree)
    /home/user/go/src/bytes (from $GOROOT)
    ($GOPATH not set. For more details see: 'go help gopath')
vendor/github.com/getsentry/raven-go/client.go:6:2: cannot find package "compress/zlib" in any of:
    /home/user/go/src/gitlab.com/company/bc_to_influx/vendor/compress/zlib (vendor tree)
    /home/user/go/src/compress/zlib (from $GOROOT)
    ($GOPATH not set. For more details see: 'go help gopath')
../../../github.com/getsentry/sentry-go/client.go:4:2: cannot find package "context" in any of:
    /home/user/go/src/context (from $GOROOT)
    ($GOPATH not set. For more details see: 'go help gopath')
vendor/github.com/getsentry/raven-go/client.go:7:2: cannot find package "crypto/rand" in any of:
    /home/user/go/src/gitlab.com/company/bc_to_influx/vendor/crypto/rand (vendor tree)
    /home/user/go/src/crypto/rand (from $GOROOT)
    ($GOPATH not set. For more details see: 'go help gopath')

等等项目所需的各种包。 会出现很多问题:

  • GOPATH 被声明为“未设置”,而它实际上设置为“/home/user/go”,正如我通过运行go env所看到的(顺便说一句,GOROOT 设置为“/home/user/go”,我相信可能会引起一些麻烦,因为我已经理解 GOPATH 和 GOROOT 应该不同)

  • go 似乎在“/home/user/go/src/bufio”中寻找诸如 bufio 之类的包,而我希望它在“/home/user/go/src/github.com/bufio”中搜索

一种解决方法是使用sudo go build ,它工作正常,但我想正确解决这个问题。 对于 root 用户,GOPATH 设置为“/root/go”,GOROOT 设置为“/snap/go/4762”。

我正在使用 govendor,并且 GO111MODULE 设置为关闭。

这都是错误的。

  1. go env不像printenv那样工作:它打印使用的值,这些值是故意设置的值或默认值。 您的 GOPATH 未设置,并且go env打印默认值。

  2. 从不,从字面上看,永远不要设置 GOROOT。 绝不。 GOROOT 适用于做专家工作的专家。 如果您有正确设置的 Go 安装,则永远不需要构建、编译、测试等等正常的 Go 代码。

  3. 永远不要sudo go whatever 须藤不是神奇的让它工作。! 锤子。

  4. govendor 的自述文件说:使用 Go 模块

别再想聪明了。 按照官方安装说明安装Go,不要乱用GOROOT。 设置或不设置 GOPATH。 如果在 GOPATH 模式下工作,则必须在编译代码之前go get依赖项。 使用 Go 模块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM