原文地址:
Go 1.16 的这个新变化需要适应下:go get 和 go install 的变化
一直以来,go get
用于下载并安装 Go
包、命令等,而 go install
在 module
时代几乎很少使用,在 GOPATH
年代,go install
用来编译安装本地项目。
自 1.16 起,官方说,不应该 go get
下载安装命令(即可执行程序),不过只是这么说,却依然可以使用。
但 Go1.17 开始,如果使用 go get
安装命令,会警告:
$ go get github.com/github/hub
go get: installing executables with 'go get' in module mode is deprecated.
To adjust and download dependencies of the current module, use 'go get -d'.
To install using requirements of the current module, use 'go install'.
To install ignoring the current module, use 'go install' with a version,
like 'go install example.com/cmd@latest'.
For more information, see https://golang.org/doc/go-get-install-deprecation
or run 'go help get' or 'go help install'.
也就是说,go get
只用来下载普通的包,安装可执行程序,应该使用 go install
。
$ go install github.com/github/hub
这会将 hub
命令安装到 $GOBIN
下。
此外,go get
有一个参数 -d
,指示 go get
下载对应的包,但不做编译和安装。将来的版本,-d
会成为默认行为,这样会更快。此外,因为不编译,即使目标依赖在特定平台编译报错,go get
也能正常执行完。
关于 Go 1.16 的变化。Reddit 上有一张图总结的挺好的:
版权声明:本文为wohu1104原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。