我试图像这样安装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 env。 如果你看到go没有安装,你可以通过homebrew或包和/或其他方式安装它。 如果您看到输出,那么您的Go已经安装。 它向你展示了所有已设置和未设置的env。

如果你看到GOROOT为空:

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

如果你看到GOPATH为空:

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

其他回答

运行go help environment,它有go env命令可以列出的每个环境变量的文档

GOPATH的讨论如下:

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

这里讨论的是GOROOT:

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

在现代围棋中,你不需要设置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/上找到

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

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

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

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

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