我正在尝试建立一些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/