我试图像这样安装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


当前回答

截至2020年和Go版本1.13+,在Windows中更新GOPATH的最佳方法是在命令提示符中输入:

setx GOPATH C:\mynewgopath

其他回答

从go 1.8(2017年第二季度)开始,GOPATH将默认设置为$HOME/go

参见第17262期和Rob Pike的评论:

$HOME/go就是这样。 没有唯一的最佳答案,但这是简短而甜蜜的,只有在$HOME/go已经存在的情况下,选择这个名称才会成为一个问题,这只会发生在已经安装了go并且理解GOPATH的专家身上。

您不需要显式地设置GOROOT(现代版本的Go可以根据您运行的Go二进制文件的位置自行确定)。

另外,当尝试使用vgo工作时,得到了跟随错误:

go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'

删除GOROOT,更新我的GOPATH和导出GO111MODULE=“on”解决了这个问题。

看这里

GOPATH可以设置为一个以冒号分隔的路径列表,其中可以找到Go代码、包对象和可执行文件。 设置GOPATH使用goinstall在Go树之外构建和安装自己的代码和外部库(并避免编写makefile)。

对于所有新手,如果你正在使用Ubuntu,他们可以简单地导出GOPATH=$HOME/go,或者去帮助GOPATH获取更多信息。

以下是我的简单设置:

directory for go related things: ~/programming/go
directory for go compiler/tools: ~/programming/go/go-1.4
directory for go software      : ~/programming/go/packages

GOROOT、GOPATH、PATH的设置如下:

export GOROOT=/home/user/programming/go/go-1.4
export GOPATH=/home/user/programming/go/packages
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

所以,简而言之:

GOROOT是编译器/工具,来自go安装。 GOPATH是为你自己的项目/第三方库(下载“go get”)。

具体到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.