Go: use go mod vendor

Go: use go mod vendor


1.Background

We manage the dependent library versions of our projects based on the go mod mechanism, where go.mod records the dependent library version information.

Insert image description here
Generally, the source code of third-party dependent libraries (including those on the company's intranet gitlab) is not included in our project. Instead, go connects to the public network and the intranet to download to the local GOPATH during compilation, and then compiles.

The problem is that sometimes it is necessary to compile the Go project without a public network or an intranet (unable to connect to the intranet gitlab). How to do this?

At this time, you need to use go mod vendor to download the project's dependent libraries into the project and compile them as part of the project.

PS:

  1. Although it is usually not possible or necessary to compile in real time in an environment without a public network or an intranet, because go is very portable and is often delivered and deployed in the form of executable files, this possibility cannot be ruled out;
  2. Prevent dependent libraries from being deleted or moved for some reason, causing dependencies to not be found and compilation to fail;
  3. For novices, it may be slightly difficult to download some external dependencies;
  4. other…

In short, our purpose is to use go mod vendor to download the project's dependent library into the project, that is, the project contains the dependent library source code. The dependent library is like a part of the project and is also subject to the project's version control (git, svn...).


2. Environment

go environment:

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 version:

Insert image description here


3.Use

Example:

Insert image description here

The project demo consists of two files:

1.main.go: The project depends on gopkg.in/yaml.v2 module (version: v2.4.0);

2.go.mod: records that the current project demo depends on yaml module;

The most common and simplest way is to directly execute go mod vendor:

Insert image description here
Execute go mod vendor, download the gopkg.in/[email protected] that this project depends on to the root directory vendor of the project demo, and organize it according to specific formats and specifications.

If you ctrl+click yaml.v2 after import at this time, it will automatically jump to yaml.v2 in the vendor directory:

Insert image description here

Instead of yaml.v2 in GOPATH:

Insert image description here
goland is reminding you that the current project uses yaml.v2 in the vendor directory in the project demo, not yaml.v2 in GOPATH.

Even at this moment, we delete yaml.v2 in GOPATH:

Insert image description here
Compile the demo directly in the project and no longer need to download the yaml.v2 dependency:

Insert image description here


4.Principle

For official documents, please see [ Important! ! ! 】:

1.https://golang.org/ref/mod#go-mod-vendor
2.https://golang.org/ref/mod#vendoring

Command line help:

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'.

Important section:

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.

If go.mod changes, go mod vendor should be re-executed!

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. Executing go mod vendor will delete the existing vendor directory in the project;

  2. Never make secondary modifications or changes to dependent libraries in the vendor!

  3. The go command does not check whether the dependent libraries in the vendor have been modified;

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.

When go version >= 1.14, if the vendor directory exists, the vendor will be automatically enabled.

-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.Reference

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

Guess you like

Origin blog.csdn.net/test1280/article/details/120855865
Go