我试图像这样安装doozer:

$ goinstall github.com/ha/doozer

我得到这些错误。

goinstall: os: go/build: package could not be found locally goinstall: fmt: go/build: package could not be found locally goinstall: io: go/build: package could not be found locally goinstall: reflect: go/build: package could not be found locally goinstall: math: go/build: package could not be found locally goinstall: rand: go/build: package could not be found locally goinstall: url: go/build: package could not be found locally goinstall: net: go/build: package could not be found locally goinstall: sync: go/build: package could not be found locally goinstall: runtime: go/build: package could not be found locally goinstall: strings: go/build: package could not be found locally goinstall: sort: go/build: package could not be found locally goinstall: strconv: go/build: package could not be found locally goinstall: bytes: go/build: package could not be found locally goinstall: log: go/build: package could not be found locally goinstall: encoding/binary: go/build: package could not be found locally


当前回答

通常不应该显式地设置GOROOT。go命令根据它自己的目录位置自动识别适当的GOROOT。


GOPATH默认为$HOME/go。只有当你想把它放在其他地方时,你才需要显式地设置它。

GOPATH包含:

使用go install安装的二进制文件,位于$GOPATH/bin.¹ 可以使用GOBIN环境变量覆盖该位置。 下载的模块源代码和校验和的缓存,位于$GOPATH/pkg/mod。 这个位置可以使用GOMODCACHE环境变量来覆盖。

如果同时设置了GOBIN和GOMODCACHE,而没有设置GO111MODULE=off,那么GOPATH本身应该基本上没有影响。


另外,在传统的GOPATH模式下(也设置了GO111MODULE=off), GOPATH包含:

用于构建包的源代码,存储在根目录树$GOPATH/src中。 使用go install安装的非二进制文件,位于$GOPATH/pkg。 安装非二进制包不再特别有用:go命令有一个构建工件的缓存,从go 1.12开始,即使是在GOPATH模式下,这也是必需的。 构建缓存不在GOPATH中。它的位置可以用GOCACHE环境变量设置。


¹二进制文件也可以在go 1.17及更早的版本上使用go get安装,但go install优先于go 1.16;见https://golang.org/doc/go1.16。

其他回答

值应该是(MACOS):

GOROOT="/usr/local/go" --> all binaries file core go
GOPATH="/Applications/proyectos/go" --> the route to workspace (custom workspace)

我读了go help gopath文档,仍然非常困惑,但从另一个go文档页面中发现了这个小块:

GOPATH环境变量指定工作空间的位置。它可能是开发Go代码时需要设置的唯一环境变量。

http://golang.org/doc/code.html#GOPATH

你可以使用一个命令:go env GOPATH

通常不应该显式地设置GOROOT。go命令根据它自己的目录位置自动识别适当的GOROOT。


GOPATH默认为$HOME/go。只有当你想把它放在其他地方时,你才需要显式地设置它。

GOPATH包含:

使用go install安装的二进制文件,位于$GOPATH/bin.¹ 可以使用GOBIN环境变量覆盖该位置。 下载的模块源代码和校验和的缓存,位于$GOPATH/pkg/mod。 这个位置可以使用GOMODCACHE环境变量来覆盖。

如果同时设置了GOBIN和GOMODCACHE,而没有设置GO111MODULE=off,那么GOPATH本身应该基本上没有影响。


另外,在传统的GOPATH模式下(也设置了GO111MODULE=off), GOPATH包含:

用于构建包的源代码,存储在根目录树$GOPATH/src中。 使用go install安装的非二进制文件,位于$GOPATH/pkg。 安装非二进制包不再特别有用:go命令有一个构建工件的缓存,从go 1.12开始,即使是在GOPATH模式下,这也是必需的。 构建缓存不在GOPATH中。它的位置可以用GOCACHE环境变量设置。


¹二进制文件也可以在go 1.17及更早的版本上使用go get安装,但go install优先于go 1.16;见https://golang.org/doc/go1.16。

具体到GOROOT, Go 1.9会自动将其设置到其安装路径。 即使你安装了多个Go,调用1.9。xone将GOROOT设置为/path/到/go/1.9(之前,如果没有设置,它假设默认路径为/usr/local/go或c:\ go)。

参见CL Go Review 53370:

The go tool will now use the path from which it was invoked to attempt to locate the root of the Go install tree. This means that if the entire Go installation is moved to a new location, the go tool should continue to work as usual. This may be overriden by setting GOROOT in the environment, which should only be done in unusual circumstances. Note that this does not affect the result of the runtime.GOROOT() function, which will continue to report the original installation location; this may be fixed in later releases.