go?install和go?get的區(qū)別實例詳解
go get 和 go install 的區(qū)別
先看結(jié)論:
go get
: 對go mod
項目,添加,更新,刪除go.mod
文件的依賴項(僅源碼)。不執(zhí)行編譯。側(cè)重應用依賴項管理。go install
: 在操作系統(tǒng)中安裝 Go 生態(tài)的第三方命令行應用。不更改項目go.mod
文件。側(cè)重可執(zhí)行文件的編譯和安裝。
之前網(wǎng)上亂傳的 go get 命令要被棄用是錯的。正確說法是,go 1.17
后,go get
命令的使用方式發(fā)生了改變.
具體什么改變呢?請看官方說明:
Starting in Go 1.17, installing executables with go get is deprecated. go install may be used instead.
In Go 1.18, go get will no longer build packages; it will only be used to add, update, or remove dependencies in go.mod.
Specifically, go get will always act as if the -d flag were enabled.
大概表達3個意思:
- 自
Go 1.17
起, 棄用go get
命令安裝可執(zhí)行文件,使用go install
命令替代. - 自
Go 1.18
起,go get
命令不再有編譯包的功能。將只有添加,更新,移除go.mod
文件中的依賴項的功能。 go get
命令將默認啟用-d
選項。
go get命變更
- Go 1.17 之前:
go get
通過遠程拉取或更新代碼包及其依賴包,并自動完成編譯和安裝
。實際分成兩步操作:1. 下載源碼包,2. 執(zhí)行 go install。 - Go 1.17 之后:
棄用go get命令的編譯和安裝功能
go get命令變更的原因
由于 go 1.11
之后 go mod
modules特性的引入,使得go get
命令,既可以安裝第三方命令,又可以從 go.mod
文件自動更新項目依賴。但多數(shù)情況下,開發(fā)者只想做二者之一。
自 go 1.16
起,go install
命令,可以忽略當前目錄的 go.mod
文件(如果存在),直接安裝指定版本的命令行應用。
go get
命令的編譯和安裝功能,因為和 go install
命令的功能重復,故被棄用。由于棄用了編譯和安裝功能,go get
命令將獲得更高的執(zhí)行效率, 也不會在更新包的時候,再出現(xiàn)編譯失敗的報錯。
Since modules were introduced, the go get command has been used both to update dependencies in go.mod and to install commands.
This combination is frequently confusing and inconvenient: in most cases, developers want to update a dependency or install a command but not both at the same time.Since Go 1.16, go install can install a command at a version specified on the command line while ignoring the go.mod file in the current directory (if one exists).
go install should now be used to install commands in most cases.go get’s ability to build and install commands is now deprecated, since that functionality is redundant with go install.
Removing this functionality will make go get faster, since it won’t compile or link packages by default.
go get also won’t report an error when updating a package that can’t be built for the current platform.
go get
由于具備更改 go.mod
文件的能力,因此我們 必須要避免執(zhí)行 go get
命令時,讓它接觸到我們的 go.mod
文件 ,否則它會將我們安裝的工具作為一個依賴。
所以,如果不是為了更新項目依賴,而是安裝可執(zhí)行命令,請使用 go install
GOMODULE常用命令
go mod init # 初始化go.mod go mod tidy # 直接從源代碼中獲取依賴關系,更新依賴文件??蓜h掉go.mod中無用的依賴。 go mod download # 下載依賴文件 go mod vendor # 將依賴轉(zhuǎn)移至本地的vendor文件 go mod edit # 手動修改依賴文件 go mod graph # 打印依賴圖 go mod verify # 校驗依賴
在項目源碼中使用 import 語句,導入新的依賴模塊前,可用 go get
命令,先下載新模塊。
go instsll 應該在module外部使用 https://github.com/golang/go/issues/40276
棄用go get命令安裝可執(zhí)行文件 https://go.dev/doc/go-get-install-deprecation
Go 1.16 中關于 go get 和 go install https://cloud.tencent.com/developer/article/1766820
總結(jié)
到此這篇關于go install和go get區(qū)別的文章就介紹到這了,更多相關go install和go get區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
GoLang jwt無感刷新與SSO單點登錄限制解除方法詳解
這篇文章主要介紹了GoLang jwt無感刷新與SSO單點登錄限制解除方法,JWT是一個簽名的JSON對象,通常用作Oauth2的Bearer token,JWT包括三個用.分割的部分。本文將利用JWT進行認證和加密,感興趣的可以了解一下2023-03-03golang?pprof監(jiān)控memory?block?mutex統(tǒng)計原理分析
這篇文章主要為大家介紹了golang?pprof監(jiān)控memory?block?mutex統(tǒng)計原理分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04