网站建设定制开发Go:go mod vendor 使用

Go:go mod vendor 使用


1.背景

我们基于 go mod 网站建设定制开发机制来管理我们项目的网站建设定制开发依赖库版本,其中 go.mod 网站建设定制开发记录了依赖库版本信息。


网站建设定制开发一般第三方依赖库(包括公司内网上的依赖库),其源码都不被包含在我们的项目内部,而是在编译的时候go连接公网、内网下载到本地GOPATH,然后编译。

问题是,有些时候需在无公网、无内网(无法连接内网gitlab)的情况下编译go项目,如何做呢?

在此时,需使用go mod vendor将项目的依赖库下载到项目内部,作为项目的一部分来编译。

PS:

  1. 虽然通常不会也不需要在无公网、无内网环境实时编译,因为go的可移植性很好,常以可执行文件方式交付部署,但并不能排除此种可能;
  2. 防止依赖库因为某种原因被删除、移动,导致找不到依赖并编译失败;
  3. 对新手来说,下载一些墙外的依赖可能略有困难;
  4. 其他…

总之,我们的目的是使用 go mod vendor,将项目的依赖库下载到项目内部,即项目中包含依赖库源码,依赖库如同项目的一部分,也受到项目的版本管控(git、svn…)。


2.环境

go环境:

D:\workspace\demo>go envset GO111MODULE=onset GOARCH=amd64set GOBIN=set GOCACHE=C:\Users\梁翠翠\AppData\Local\go-buildset GOENV=C:\Users\梁翠翠\AppData\Roaming\go\envset GOEXE=.exeset GOEXPERIMENT=set GOFLAGS=set GOHOSTARCH=amd64set GOHOSTOS=windowsset GOINSECURE=set GOMODCACHE=C:\gopath\pkg\modset GONOPROXY=gitlab.ebupt.comset GONOSUMDB=gitlab.ebupt.comset GOOS=windowsset GOPATH=C:\gopathset GOPRIVATE=gitlab.ebupt.comset GOPROXY=https://goproxy.ioset GOROOT=C:\Program Files\Goset GOSUMDB=sum.golang.orgset GOTMPDIR=set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64set GOVCS=set GOVERSION=go1.17.2set GCCGO=gccgoset AR=arset CC=gccset CXX=g++set CGO_ENABLED=1set GOMOD=D:\workspace\demo\go.modset CGO_CFLAGS=-g -O2set CGO_CPPFLAGS=set CGO_CXXFLAGS=-g -O2set CGO_FFLAGS=-g -O2set CGO_LDFLAGS=-g -O2set PKG_CONFIG=pkg-configset GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\梁翠翠\AppData\Local\Temp\go-build648873300=/tmp/go-build -gno-record-gcc-switches
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

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/yaml.v2@v2.4.0下载到项目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.
2.

命令行帮助:

D:\workspace\demo>go help mod vendorusage: go mod vendor [-e] [-v]Vendor resets the main module's vendor directory to include all packagesneeded 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 vendoredmodules and packages to standard error.The -e flag causes vendor to attempt to proceed despite errorsencountered while loading packages.See https://golang.org/ref/mod#go-mod-vendor for more about 'go mod vendor'.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

关键部分:

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
  • 1
  • 2
  • 3

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.参考





网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发