Go語言中g(shù)o?mod?vendor使用方法
1.背景
我們基于 go mod 機(jī)制來管理我們項(xiàng)目的依賴庫版本,其中 go.mod 記錄了依賴庫版本信息。
一般第三方依賴庫(包括公司內(nèi)網(wǎng)gitlab上的依賴庫),其源碼都不被包含在我們的項(xiàng)目內(nèi)部,而是在編譯的時候go連接公網(wǎng)、內(nèi)網(wǎng)下載到本地GOPATH,然后編譯。
問題是,有些時候需在無公網(wǎng)、無內(nèi)網(wǎng)(無法連接內(nèi)網(wǎng)gitlab)的情況下編譯go項(xiàng)目,如何做呢?
在此時,需使用go mod vendor將項(xiàng)目的依賴庫下載到項(xiàng)目內(nèi)部,作為項(xiàng)目的一部分來編譯。
PS:
- 雖然通常不會也不需要在無公網(wǎng)、無內(nèi)網(wǎng)環(huán)境實(shí)時編譯,因?yàn)間o的可移植性很好,常以可執(zhí)行文件方式交付部署,但并不能排除此種可能;
- 防止依賴庫因?yàn)槟撤N原因被刪除、移動,導(dǎo)致找不到依賴并編譯失敗;
- 對新手來說,下載一些墻外的依賴可能略有困難;
- 其他…
總之,我們的目的是使用 go mod vendor,將項(xiàng)目的依賴庫下載到項(xiàng)目內(nèi)部,即項(xiàng)目中包含依賴庫源碼,依賴庫如同項(xiàng)目的一部分,也受到項(xiàng)目的版本管控(git、svn…)。
2.環(huán)境
go環(huán)境:
D:\workspace\demo>go env set GO111MODULE=on set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\梁翠翠\AppData\Local\go-build set GOENV=C:\Users\梁翠翠\AppData\Roaming\go\env set GOEXE=.exe set GOEXPERIMENT= set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOINSECURE= set GOMODCACHE=C:\gopath\pkg\mod set GONOPROXY=gitlab.ebupt.com set GONOSUMDB=gitlab.ebupt.com set GOOS=windows set GOPATH=C:\gopath set GOPRIVATE=gitlab.ebupt.com set GOPROXY=https://goproxy.io set GOROOT=C:\Program Files\Go set GOSUMDB=sum.golang.org set GOTMPDIR= set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64 set GOVCS= set GOVERSION=go1.17.2 set GCCGO=gccgo set AR=ar set CC=gcc set CXX=g++ set CGO_ENABLED=1 set GOMOD=D:\workspace\demo\go.mod set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\梁翠翠\AppData\Local\Temp\go-build648873300=/tmp/go-build -gno-record-gcc-switches
goland版本:
3.使用
示例:
項(xiàng)目demo由兩個文件構(gòu)成:
1.main.go:項(xiàng)目依賴gopkg.in/yaml.v2 module(版本:v2.4.0);
2.go.mod:記錄當(dāng)前項(xiàng)目demo依賴yaml module;
最常用、最簡單的辦法是,直接執(zhí)行g(shù)o mod vendor:
執(zhí)行g(shù)o mod vendor,將此項(xiàng)目依賴的gopkg.in/yaml.v2@v2.4.0下載到項(xiàng)目demo的根目錄vendor中,并按照特定格式、規(guī)范組織。
如果此時你ctrl+鼠標(biāo)點(diǎn)擊import后面的yaml.v2時,將自動跳轉(zhuǎn)到vendor目錄下的yaml.v2:
而不再是GOPATH中的yaml.v2:
goland在提示你,當(dāng)前項(xiàng)目使用的是項(xiàng)目demo中vendor目錄下得yaml.v2,而非GOPATH中的yaml.v2。
即使此刻,我們將GOPATH中的yaml.v2刪除:
在項(xiàng)目中直接編譯demo,不再需要下載yaml.v2依賴:
4.原理
官方文檔請參見【重要?。?!】:
1.https://golang.org/ref/mod#go-mod-vendor
2.https://golang.org/ref/mod#vendoring
命令行幫助:
D:\workspace\demo>go help mod vendor usage: go mod vendor [-e] [-v] Vendor resets the main module's vendor directory to include all packages needed to build and test all the main module's packages. It does not include test code for vendored packages. The -v flag causes vendor to print the names of vendored modules and packages to standard error. The -e flag causes vendor to attempt to proceed despite errors encountered while loading packages. See https://golang.org/ref/mod#go-mod-vendor for more about 'go mod vendor'.
關(guān)鍵部分:
1.The go mod vendor command constructs a directory named vendor in the main module’s root directory that contains copies of all packages needed to support builds and tests of packages in the main module.
2.When vendoring is enabled, the go command will load packages from the vendor directory instead of downloading modules from their sources into the module cache and using packages those downloaded copies.
3.If go.mod changed since vendor/modules.txt was generated, go mod vendor should be run again.
如果 go.mod 發(fā)生變化,應(yīng)當(dāng)重新執(zhí)行 go mod vendor!
4.Note that go mod vendor removes the vendor directory if it exists before re-constructing it. Local changes should not be made to vendored packages. The go command does not check that packages in the vendor directory have not been modified, but one can verify the integrity of the vendor directory by running go mod vendor and checking that no changes were made.
- 執(zhí)行g(shù)o mod vendor將刪除項(xiàng)目中已存在的vendor目錄;
- 永遠(yuǎn)不要對vendor中的依賴庫進(jìn)行二次修改、更改!
- go命令不檢查vendor中的依賴庫是否被修改;
5.If the vendor directory is present in the main module’s root directory, it will be used automatically if the go version in the main module’s go.mod file is 1.14 or higher. To explicitly enable vendoring, invoke the go command with the flag -mod=vendor. To disable vendoring, use the flag -mod=readonly or -mod=mod.
在go version >= 1.14時,如果存在vendor目錄,將自動啟用vendor。
-mod=vendor -mod=readonly -mod=mod
6.When vendoring is enabled, build commands like go build and go test load packages from the vendor directory instead of accessing the network or the local module cache.
5.參考
- https://golang.org/ref/mod#go-mod-vendor
- https://golang.org/ref/mod#vendoring
- https://yanbin.blog/go-use-go-mod-manage-dependencies/
- https://cloud.tencent.com/developer/article/1626849
- https://cloud.tencent.com/developer/article/1604866
到此這篇關(guān)于Go語言中g(shù)o mod vendor使用方法的文章就介紹到這了,更多相關(guān)go mod vendor使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang?pprof?監(jiān)控goroutine?thread統(tǒng)計(jì)原理詳解
這篇文章主要為大家介紹了golang?pprof?監(jiān)控goroutine?thread統(tǒng)計(jì)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04go語言的panic和recover函數(shù)用法實(shí)例
今天小編就為大家分享一篇關(guān)于go語言的panic和recover函數(shù)用法實(shí)例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04golang 一次性定時器Timer用法及實(shí)現(xiàn)原理詳解
這篇文章主要為大家介紹了golang 一次性定時器Timer用法及實(shí)現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08GOLANG使用Context實(shí)現(xiàn)傳值、超時和取消的方法
這篇文章主要介紹了GOLANG使用Context實(shí)現(xiàn)傳值、超時和取消的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01