我试图像这样安装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或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/上找到

其他回答

不建议使用GOPATH和GOROOT配置。

您可以使用GO模块。

例如:

mkdir go_app
cd go_app
go mod init go_app

GOPATH不应该指向Go安装,而是指向您的工作空间(参见https://golang.org/doc/code.html#GOPATH)。当你使用go get或go install安装某个包时,它会落在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.

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

如果你看到GOROOT为空:

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

如果你看到GOPATH为空:

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

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

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

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