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


当前回答

GOPATH的讨论如下:

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

这里讨论的是GOROOT:

$GOROOT Go树的根,通常是$HOME/ Go。默认为 目录的父目录。Bash运行。如果你选择不去的话 设置$GOROOT时,必须运行gomake而不是make或gmake 使用传统的makefile开发Go程序。

其他回答

答案很多,但没有实质内容,就像机器人对系统中的内容进行剪切和粘贴。不需要将GOROOT设置为环境变量。但是,有必要设置GOPATH环境变量,如果没有设置,则默认为${HOME}/go/ folder。

您必须注意的是PATH环境变量,因为这个变量可以改变您的go版本。不是GOROOT !忘记GOROOT。

现在,如果你切换或更改到一个新的go版本,你下载的包将使用默认的$HOME/go文件夹,它将与你之前的go版本混合。这可不太好。

因此,这就是需要定义GOPATH的地方,以便隔离新go版本的下载包。

总之,忘掉GOROOT吧。多想想GOPATH。

如上所述:

GOPATH环境变量指定您的 工作区。

对于Windows,这对我来说是有效的(在Ms-dos窗口中):

set GOPATH=D:\my_folder_for_go_code\

这将创建一个Ms-dos识别的GOPATH变量,使用如下:

cd %GOPATH%

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

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

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

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

setx GOPATH C:\mynewgopath

在cmd/go文档中讨论了GOPATH:

GOPATH环境变量列出了寻找Go代码的位置。在 Unix,值是一个冒号分隔的字符串。Windows操作系统为 以分号分隔的字符串。在Plan 9中,取值为列表。 必须将GOPATH设置为在外部获取、构建和安装包 标准围棋树。

安装说明中讨论了GOROOT:

The Go binary distributions assume they will be installed in /usr/local/go (or c:\Go under Windows), but it is possible to install the Go tools to a different location. In this case you must set the GOROOT environment variable to point to the directory in which it was installed. For example, if you installed Go to your home directory you should add the following commands to $HOME/.profile: export GOROOT=$HOME/go export PATH=$PATH:$GOROOT/bin Note: GOROOT must be set only when installing to a custom location.

(更新版的克里斯·邦奇的回答。)