Go語言中go mod vendor使用方法

1.背景

我們基於 go mod 機制來管理我們項目的依賴庫版本,其中 go.mod 記錄瞭依賴庫版本信息。

一般第三方依賴庫(包括公司內網gitlab上的依賴庫),其源碼都不被包含在我們的項目內部,而是在編譯的時候go連接公網、內網下載到本地GOPATH,然後編譯。

問題是,有些時候需在無公網、無內網(無法連接內網gitlab)的情況下編譯go項目,如何做呢?

在此時,需使用go mod vendor將項目的依賴庫下載到項目內部,作為項目的一部分來編譯。

PS:

  • 雖然通常不會也不需要在無公網、無內網環境實時編譯,因為go的可移植性很好,常以可執行文件方式交付部署,但並不能排除此種可能;
  • 防止依賴庫因為某種原因被刪除、移動,導致找不到依賴並編譯失敗;
  • 對新手來說,下載一些墻外的依賴可能略有困難;
  • 其他…

總之,我們的目的是使用 go mod vendor,將項目的依賴庫下載到項目內部,即項目中包含依賴庫源碼,依賴庫如同項目的一部分,也受到項目的版本管控(git、svn…)。

2.環境

go環境:

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.使用

示例:

項目demo由兩個文件構成:

1.main.go:項目依賴gopkg.in/yaml.v2 module(版本:v2.4.0);

2.go.mod:記錄當前項目demo依賴yaml module;

最常用、最簡單的辦法是,直接執行go mod vendor:

執行go mod vendor,將此項目依賴的gopkg.in/[email protected]下載到項目demo的根目錄vendor中,並按照特定格式、規范組織。

如果此時你ctrl+鼠標點擊import後面的yaml.v2時,將自動跳轉到vendor目錄下的yaml.v2:

而不再是GOPATH中的yaml.v2:

goland在提示你,當前項目使用的是項目demo中vendor目錄下得yaml.v2,而非GOPATH中的yaml.v2。

即使此刻,我們將GOPATH中的yaml.v2刪除:

在項目中直接編譯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'.

關鍵部分:

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 發生變化,應當重新執行 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.

  1. 執行go mod vendor將刪除項目中已存在的vendor目錄;
  2. 永遠不要對vendor中的依賴庫進行二次修改、更改!
  3. 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

到此這篇關於Go語言中go mod vendor使用方法的文章就介紹到這瞭,更多相關go mod vendor使用內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: