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


当前回答

如果你正在使用发行版go,你应该指向包含文件的位置,例如:

$ rpm -ql golang | grep include
/usr/lib/golang/include

(这是针对Fedora 20)

其他回答

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

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

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

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

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

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

在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.

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

在现代围棋中,你不需要设置GOPATH或GOROOT。事实上,除非您正在做一些非常专业的事情,否则最好在您的系统上不设置它们。

使用Go模块。安装Go之后,选择一个您想要工作的目录。然后:

$ mkdir example
$ cd example
$ go mod init example.com

注意,模块名example.com是任意的;如果你把你的工作放在GitHub上,这可能是github.com/your-username/project-name。

最后一个命令将创建一个go。国防部文件;现在你可以用go get获取依赖项:

$ go get rsc.io/quote

现在你的代码使用这个依赖:

$ touch main.go

把它放在main.go中:

package main

import (
    "fmt"

    "rsc.io/quote"
)

func main() {
    fmt.Println(quote.Go())
}

并运行:

$ go run .

W.r.t.原创问题,你现在可以得到你的doozer依赖:

$ go get github.com/ha/doozer

现在您可以在代码中使用doozer模块。等等。你也可以检查一下。Mod文件在您的目录中查看所列出的依赖项,以及它们的版本。每个模块都是自包含的,有自己的依赖版本。你可以有两个并排的模块,每个模块都有自己的功能。mod文件指向一些依赖的不同版本-这将工作正常,因为模块之间的隔离。

要了解更多信息,请从这里的官方教程开始。在几个章节中,它将引导您完成上述步骤,以及编写自己的可重用模块和包,并从其他模块导入它们。更多的交互式教程可以在https://play-with-go.dev/上找到

在osx中,我安装了brew,下面是适合我的设置

GOPATH="$HOME/my_go_work_space" //make sure you have this folder created

GOROOT="/usr/local/Cellar/go/1.10/libexec"