我试图像这样安装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可以根据您运行的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)。

其他回答

还有一种情况是,当我们使用go时,它会编译所有的go文件。

假设我们有一个文件main。然后我们将当前文件更改为main_old。Go,然后添加新的main。文件。然后当我们构建应用程序时,所有的go文件都将被编译。所以发生的错误可能是由于其他go文件中的编译错误。

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

对于所有新手,如果你正在使用Ubuntu,他们可以简单地导出GOPATH=$HOME/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)。

第一次运行go env。 如果你看到go没有安装,你可以通过homebrew或包和/或其他方式安装它。 如果您看到输出,那么您的Go已经安装。 它向你展示了所有已设置和未设置的env。

如果你看到GOROOT为空:

在我的电脑上:/usr/local/go/bin/go 然后export GOROOT=/usr/local/go

如果你看到GOPATH为空:

在计算机的任何位置为go项目创建任意目录,在我的例子中是:~/GO_PROJECTS 然后导出GOPATH=~/GO_PROJECTS