go?install和go?get的區(qū)別實(shí)例詳解
go get 和 go install 的區(qū)別
先看結(jié)論:
go get
: 對go mod
項(xiàng)目,添加,更新,刪除go.mod
文件的依賴項(xiàng)(僅源碼)。不執(zhí)行編譯。側(cè)重應(yīng)用依賴項(xiàng)管理。go install
: 在操作系統(tǒng)中安裝 Go 生態(tài)的第三方命令行應(yīng)用。不更改項(xiàng)目go.mod
文件。側(cè)重可執(zhí)行文件的編譯和安裝。
之前網(wǎng)上亂傳的 go get 命令要被棄用是錯(cuò)的。正確說法是,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.
大概表達(dá)3個(gè)意思:
- 自
Go 1.17
起, 棄用go get
命令安裝可執(zhí)行文件,使用go install
命令替代. - 自
Go 1.18
起,go get
命令不再有編譯包的功能。將只有添加,更新,移除go.mod
文件中的依賴項(xiàng)的功能。 go get
命令將默認(rèn)啟用-d
選項(xiàng)。
go get命變更
- Go 1.17 之前:
go get
通過遠(yuǎn)程拉取或更新代碼包及其依賴包,并自動完成編譯和安裝
。實(shí)際分成兩步操作:1. 下載源碼包,2. 執(zhí)行 go install。 - Go 1.17 之后:
棄用go get命令的編譯和安裝功能
go get命令變更的原因
由于 go 1.11
之后 go mod
modules特性的引入,使得go get
命令,既可以安裝第三方命令,又可以從 go.mod
文件自動更新項(xiàng)目依賴。但多數(shù)情況下,開發(fā)者只想做二者之一。
自 go 1.16
起,go install
命令,可以忽略當(dāng)前目錄的 go.mod
文件(如果存在),直接安裝指定版本的命令行應(yīng)用。
go get
命令的編譯和安裝功能,因?yàn)楹?go install
命令的功能重復(fù),故被棄用。由于棄用了編譯和安裝功能,go get
命令將獲得更高的執(zhí)行效率, 也不會在更新包的時(shí)候,再出現(xiàn)編譯失敗的報(bào)錯(cuò)。
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
命令時(shí),讓它接觸到我們的 go.mod
文件 ,否則它會將我們安裝的工具作為一個(gè)依賴。
所以,如果不是為了更新項(xiàng)目依賴,而是安裝可執(zhí)行命令,請使用 go install
GOMODULE常用命令
go mod init # 初始化go.mod go mod tidy # 直接從源代碼中獲取依賴關(guān)系,更新依賴文件。可刪掉go.mod中無用的依賴。 go mod download # 下載依賴文件 go mod vendor # 將依賴轉(zhuǎn)移至本地的vendor文件 go mod edit # 手動修改依賴文件 go mod graph # 打印依賴圖 go mod verify # 校驗(yàn)依賴
在項(xiàng)目源碼中使用 import 語句,導(dǎo)入新的依賴模塊前,可用 go get
命令,先下載新模塊。
go instsll 應(yīng)該在module外部使用 https://github.com/golang/go/issues/40276
棄用go get命令安裝可執(zhí)行文件 https://go.dev/doc/go-get-install-deprecation
Go 1.16 中關(guān)于 go get 和 go install https://cloud.tencent.com/developer/article/1766820
總結(jié)
到此這篇關(guān)于go install和go get區(qū)別的文章就介紹到這了,更多相關(guān)go install和go get區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
GoLang jwt無感刷新與SSO單點(diǎn)登錄限制解除方法詳解
這篇文章主要介紹了GoLang jwt無感刷新與SSO單點(diǎn)登錄限制解除方法,JWT是一個(gè)簽名的JSON對象,通常用作Oauth2的Bearer token,JWT包括三個(gè)用.分割的部分。本文將利用JWT進(jìn)行認(rèn)證和加密,感興趣的可以了解一下2023-03-03使用golang實(shí)現(xiàn)在屏幕上打印進(jìn)度條的操作
這篇文章主要介紹了使用golang實(shí)現(xiàn)在屏幕上打印進(jìn)度條的操作,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03golang?pprof監(jiān)控memory?block?mutex統(tǒng)計(jì)原理分析
這篇文章主要為大家介紹了golang?pprof監(jiān)控memory?block?mutex統(tǒng)計(jì)原理分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04