Go1.17 新特性:Go Get 變了
大家好,我是 polarisxu。
為什么把 Go 的一些小變化單獨(dú)寫(xiě)文章,而不是一篇文章介紹所有的變化?主要是想讓大家對(duì)某一個(gè)特性有更深的記憶。全部列出,很容易一眼而過(guò),過(guò)段時(shí)間就忘記了。但一個(gè)變化,專門一篇文章介紹,更容易記住。
01 安裝命令會(huì)警告
一直以來(lái),go get 用于下載并安裝 Go 包、命令等,而 go install 在 module 時(shí)代幾乎很少使用,在 GOPATH 年代,go install 用來(lái)編譯安裝本地項(xiàng)目。
自 1.16 起,官方說(shuō),不應(yīng)該 go get 下載安裝命令(即可執(zhí)行程序),不過(guò)只是這么說(shuō),卻依然可以使用。
但 Go1.17 開(kāi)始,如果使用 go get 安裝命令,會(huì)警告:
- $ 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'.
也就是說(shuō),go get 只用來(lái)下載普通的包,安裝可執(zhí)行程序,應(yīng)該使用 go install。
- $ go install github.com/github/hub
這會(huì)將 hub 命令安裝到 $GOBIN 下。
此外,go get 有一個(gè) flag -d,指示 go get 下載對(duì)應(yīng)的包,但不做編譯和安裝。將來(lái)的版本,-d 會(huì)成為默認(rèn)行為,這樣會(huì)更快。此外,因?yàn)椴痪幾g,即使目標(biāo)依賴在特定平臺(tái)編譯報(bào)錯(cuò),go get 也能正常執(zhí)行完。
至于為什么用 go install 代替 go get 執(zhí)行命令安裝,這里有詳細(xì)的說(shuō)明:https://docs.studygolang.com/doc/go-get-install-deprecation,簡(jiǎn)單說(shuō)就是和命令的語(yǔ)義更符合。
告訴大家一個(gè)參與開(kāi)源項(xiàng)目的機(jī)會(huì):
如果某個(gè)項(xiàng)目提供了怎么安裝可執(zhí)行文件的方法,大概率使用的是 go get,你可以提交一個(gè) PR,將其改為 go install,哈哈哈~
02 廢棄 -insecure
go get 的這個(gè) flag 使用的人可能不多。什么時(shí)候會(huì)用到呢?Go1.16 版本關(guān)于這個(gè) flag 的說(shuō)明:
The -insecure flag permits fetching from repositories and resolving custom domains using insecure schemes such as HTTP, and also bypassess module sum validation using the checksum database. Use with caution. This flag is deprecated and will be removed in a future version of go. To permit the use of insecure schemes, use the GOINSECURE environment variable instead. To bypass module sum validation, use GOPRIVATE or GONOSUMDB. See 'go help environment' for details.
這主要用來(lái)處理私有倉(cāng)庫(kù)沒(méi)有提供 HTTPS 的情況,同時(shí)避免進(jìn)行數(shù)據(jù)庫(kù)校驗(yàn)和檢查。不過(guò)更建議使用 GOINSECURE 環(huán)境變量??纯催@個(gè)環(huán)境變量的說(shuō)明:
GOINSECURE Comma-separated list of glob patterns (in the syntax of Go's path.Match) of module path prefixes that should always be fetched in an insecure manner. Only applies to dependencies that are being fetched directly. Unlike the -insecure flag on 'go get', GOINSECURE does not disable checksum database validation. GOPRIVATE or GONOSUMDB may be used to achieve that.
Go1.17 直接廢棄了 -insecure 這個(gè) flag,必須使用 GOINSECURE 環(huán)境變量。但這個(gè)環(huán)境變量不會(huì)禁用數(shù)據(jù)庫(kù)校驗(yàn)和檢查。
因此,對(duì)于私有倉(cāng)庫(kù),如果沒(méi)有提供 HTTPS,應(yīng)該配置 GOINSECURE,指明哪些地址啟用 INSECURE 模式,同時(shí)配置 GOPRIVATE 環(huán)境變量,避免數(shù)據(jù)庫(kù)校驗(yàn)和檢查。
- $ go get -insecure github.com/labstack/echo/v4
- go get: -insecure flag is no longer supported; use GOINSECURE instead
03 總結(jié)
建議你實(shí)際動(dòng)手試試 go get 命令,同時(shí)切換不同的 Go 版本,看看效果,以加深印象。對(duì)其中有任何疑問(wèn),都可以通過(guò) go 命令的相關(guān)幫助找到。比如查看具體環(huán)境變量的意思,可以 go help environment 查看 Go 提供的所有環(huán)境變量。