我正在尝试构建一些 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')
等等项目所需的各种包。
会出现很多问题:
go env
看到的那样(顺便说一句,GOROOT 设置为“/home/user/go”,我相信可能会引起一些麻烦,因为我已经理解 GOPATH 和 GOROOT 应该不同)一种解决方法是使用
sudo go build
,它工作正常,但我想正确解决这个问题。对于 root 用户,GOPATH 设置为“/root/go”,GOROOT 设置为“/snap/go/4762”。我正在使用 govendor,并且 GO111MODULE 设置为关闭。
最佳答案
这都是错误的。
go env
不是 像 printenv
一样工作:它打印使用的值,这些值是故意设置的值或默认值。您的 GOPATH 未设置,go env
打印默认值。 sudo go whatever
.须藤不是神奇的让它工作!锤子。 别再想聪明了。按照官方安装说明安装Go,不要乱用GOROOT。设置或不设置 GOPATH。如果在 GOPATH 模式下工作,您 必须
go get
在编译您的代码之前,您自己的依赖项。使用 Go 模块。
关于go - 使用 go build 时如何修复 'GOPATH not set' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58709523/