Go1-17 新特性:go get 變了 - 閱坊

文章推薦指數: 80 %
投票人數:10人

go get 的這個flag 使用的人可能不多。

什麼時候會用到呢?Go1.16 版本關於這個flag 的說明:. The -insecure flag permits fetching from repositories ... ✕ Rust 架構 機器學習 Java 大數據 Javascript Go 微服務 DDD Twitter Facebook Feed 服務協議 聯繫我們 大家好,我是polarisxu。

爲什麼把Go的一些小變化單獨寫文章,而不是一篇文章介紹所有的變化?主要是想讓大家對某一個特性有更深的記憶。

全部列出,很容易一眼而過,過段時間就忘記了。

但一個變化,專門一篇文章介紹,更容易記住。

01安裝命令會警告 一直以來,goget用於下載並安裝Go包、命令等,而goinstall在module時代幾乎很少使用,在GOPATH年代,goinstall用來編譯安裝本地項目。

自1.16起,官方說,不應該goget下載安裝命令(即可執行程序),不過只是這麼說,卻依然可以使用。

但Go1.17開始,如果使用goget安裝命令,會警告: $ 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'. 也就是說,goget只用來下載普通的包,安裝可執行程序,應該使用goinstall。

$ go install github.com/github/hub 這會將hub命令安裝到$GOBIN下。

此外,goget有一個flag-d,指示goget下載對應的包,但不做編譯和安裝。

將來的版本,-d會成爲默認行爲,這樣會更快。

此外,因爲不編譯,即使目標依賴在特定平臺編譯報錯,goget也能正常執行完。

至於爲什麼用goinstall代替goget執行命令安裝,這裏有詳細的說明:https://docs.studygolang.com/doc/go-get-install-deprecation,簡單說就是和命令的語義更符合。

告訴大家一個參與開源項目的機會: 如果某個項目提供了怎麼安裝可執行文件的方法,大概率使用的是goget,你可以提交一個PR,將其改爲goinstall,哈哈哈~ 02廢棄-insecure goget的這個flag使用的人可能不多。

什麼時候會用到呢?Go1.16版本關於這個flag的說明: The-insecureflagpermitsfetchingfromrepositoriesandresolvingcustomdomainsusinginsecureschemessuchasHTTP,andalsobypassessmodulesumvalidationusingthechecksumdatabase.Usewithcaution.Thisflagisdeprecatedandwillberemovedinafutureversionofgo.Topermittheuseofinsecureschemes,usetheGOINSECUREenvironmentvariableinstead.Tobypassmodulesumvalidation,useGOPRIVATEorGONOSUMDB.See'gohelpenvironment'fordetails. 這主要用來處理私有倉庫沒有提供HTTPS的情況,同時避免進行數據庫校驗和檢查。

不過更建議使用GOINSECURE環境變量。

看看這個環境變量的說明: GOINSECUREComma-separatedlistofglobpatterns(inthesyntaxofGo'spath.Match)ofmodulepathprefixesthatshouldalwaysbefetchedinaninsecuremanner.Onlyappliestodependenciesthatarebeingfetcheddirectly.Unlikethe-insecureflagon'goget',GOINSECUREdoesnotdisablechecksumdatabasevalidation.GOPRIVATEorGONOSUMDBmaybeusedtoachievethat. Go1.17直接廢棄了-insecure這個flag,必須使用GOINSECURE環境變量。

但這個環境變量不會禁用數據庫校驗和檢查。

因此,對於私有倉庫,如果沒有提供HTTPS,應該配置GOINSECURE,指明哪些地址啓用INSECURE模式,同時配置GOPRIVATE環境變量,避免數據庫校驗和檢查。

$ go get -insecure github.com/labstack/echo/v4 go get: -insecure flag is no longer supported; use GOINSECURE instead 03總結 建議你實際動手試試goget命令,同時切換不同的Go版本,看看效果,以加深印象。

對其中有任何疑問,都可以通過go命令的相關幫助找到。

比如查看具體環境變量的意思,可以gohelpenvironment查看Go提供的所有環境變量。

本文由Readfog進行AMP轉碼,版權歸原作者所有。

來源:https://mp.weixin.qq.com/s/jyr2mdq7hAOy7wKpBJHMgQ 2021年09月06日 #變量 #編譯 #環境 #校驗 #使用 猜你喜歡 Go1-17新特性:新版構建約束 Go1-17新特性:對Go依賴管理的一把大剪刀 Go1-17新特性:testing包的相關變化 Go包和命名 Go:監控模式 Go流程控制 Go語言函數 Go語言工具 Go平滑重啓 Go函數整理 Go語言之禪 Go中的併發



請為這篇文章評分?