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.
- 执行go mod vendor将删除项目中已存在的vendor目录;
- 永远不要对vendor中的依赖库进行二次修改、更改!
- 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使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!