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