Go語(yǔ)言中g(shù)o?mod?vendor使用方法
1.背景
我們基于 go mod 機(jī)制來(lái)管理我們項(xiàng)目的依賴(lài)庫(kù)版本,其中 go.mod 記錄了依賴(lài)庫(kù)版本信息。

一般第三方依賴(lài)庫(kù)(包括公司內(nèi)網(wǎng)gitlab上的依賴(lài)庫(kù)),其源碼都不被包含在我們的項(xiàng)目?jī)?nèi)部,而是在編譯的時(shí)候go連接公網(wǎng)、內(nèi)網(wǎng)下載到本地GOPATH,然后編譯。
問(wèn)題是,有些時(shí)候需在無(wú)公網(wǎng)、無(wú)內(nèi)網(wǎng)(無(wú)法連接內(nèi)網(wǎng)gitlab)的情況下編譯go項(xiàng)目,如何做呢?
在此時(shí),需使用go mod vendor將項(xiàng)目的依賴(lài)庫(kù)下載到項(xiàng)目?jī)?nèi)部,作為項(xiàng)目的一部分來(lái)編譯。
PS:
- 雖然通常不會(huì)也不需要在無(wú)公網(wǎng)、無(wú)內(nèi)網(wǎng)環(huán)境實(shí)時(shí)編譯,因?yàn)間o的可移植性很好,常以可執(zhí)行文件方式交付部署,但并不能排除此種可能;
- 防止依賴(lài)庫(kù)因?yàn)槟撤N原因被刪除、移動(dòng),導(dǎo)致找不到依賴(lài)并編譯失?。?/li>
- 對(duì)新手來(lái)說(shuō),下載一些墻外的依賴(lài)可能略有困難;
- 其他…
總之,我們的目的是使用 go mod vendor,將項(xiàng)目的依賴(lài)庫(kù)下載到項(xiàng)目?jī)?nèi)部,即項(xiàng)目中包含依賴(lài)庫(kù)源碼,依賴(lài)庫(kù)如同項(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è)文件構(gòu)成:
1.main.go:項(xiàng)目依賴(lài)gopkg.in/yaml.v2 module(版本:v2.4.0);
2.go.mod:記錄當(dāng)前項(xiàng)目demo依賴(lài)yaml module;
最常用、最簡(jiǎn)單的辦法是,直接執(zhí)行g(shù)o mod vendor:

執(zhí)行g(shù)o mod vendor,將此項(xiàng)目依賴(lài)的gopkg.in/yaml.v2@v2.4.0下載到項(xiàng)目demo的根目錄vendor中,并按照特定格式、規(guī)范組織。
如果此時(shí)你ctrl+鼠標(biāo)點(diǎn)擊import后面的yaml.v2時(shí),將自動(dòng)跳轉(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依賴(lài):

4.原理
官方文檔請(qǐng)參見(jiàn)【重要!?。?/strong>】:
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)不要對(duì)vendor中的依賴(lài)庫(kù)進(jìn)行二次修改、更改!
- go命令不檢查vendor中的依賴(lài)庫(kù)是否被修改;
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時(shí),如果存在vendor目錄,將自動(dòng)啟用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語(yǔ)言中g(shù)o mod vendor使用方法的文章就介紹到這了,更多相關(guān)go mod vendor使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang基于errgroup實(shí)現(xiàn)并發(fā)調(diào)用的方法
這篇文章主要介紹了golang基于errgroup實(shí)現(xiàn)并發(fā)調(diào)用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
golang將多路復(fù)異步io轉(zhuǎn)成阻塞io的方法詳解
常見(jiàn)的IO模型有阻塞、非阻塞、IO多路復(fù)用,異,下面這篇文章主要給大家介紹了關(guān)于golang將多路復(fù)異步io轉(zhuǎn)成阻塞io的方法,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
Go語(yǔ)言變量與基礎(chǔ)數(shù)據(jù)類(lèi)型詳情
Go 是靜態(tài)(編譯型)語(yǔ)言,是區(qū)別于解釋型語(yǔ)言的弱類(lèi)型語(yǔ)言(靜態(tài):類(lèi)型固定,強(qiáng)類(lèi)型:不同類(lèi)型不允許直接運(yùn)算),下面文章將對(duì)其進(jìn)行詳細(xì)介紹,需要的朋友可以參考一下2021-09-09
使用Go語(yǔ)言實(shí)現(xiàn)跨域資源共享(CORS)設(shè)置
在Web開(kāi)發(fā)中,跨域資源共享(CORS)是一種重要的安全機(jī)制,它允許許多資源在一個(gè)網(wǎng)頁(yè)上被另一個(gè)來(lái)源的網(wǎng)頁(yè)所訪(fǎng)問(wèn),然而,出于安全考慮,瀏覽器默認(rèn)禁止這種跨域訪(fǎng)問(wèn),為了解決這個(gè)問(wèn)題,我們可以使用Go語(yǔ)言來(lái)設(shè)置CORS,需要的朋友可以參考下2024-06-06
Go strconv包實(shí)現(xiàn)字符串和基本數(shù)據(jù)類(lèi)型轉(zhuǎn)換的實(shí)例詳解
在Go語(yǔ)言(Golang)的編程實(shí)踐中,strconv包是一個(gè)非常重要的標(biāo)準(zhǔn)庫(kù),它提供了在基本數(shù)據(jù)類(lèi)型(如整型、浮點(diǎn)型、布爾型)和字符串之間的轉(zhuǎn)換功能,本文給大家介紹了關(guān)于Go語(yǔ)言字符串轉(zhuǎn)換strconv,需要的朋友可以參考下2024-09-09
詳解Go語(yǔ)言中select語(yǔ)句的常見(jiàn)用法
這篇文章主要是來(lái)和大家介紹一下Go語(yǔ)言中select?語(yǔ)句的常見(jiàn)用法,以及在使用過(guò)程中的注意事項(xiàng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-07-07
Go語(yǔ)言處理超大字符串型整數(shù)加減經(jīng)典面試詳解
這篇文章主要為大家介紹了Go語(yǔ)言處理超大字符串型整數(shù)加減經(jīng)典面試示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

