Go语言中go mod vendor使用方法

 更新时间:2022年10月25日 16:06:33   作者:test1280  
go mod vendor的功能是将新增的依赖包自动写入当前项目的 vendor目录,下面这篇文章主要给大家介绍了关于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/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.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使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Golang交叉编译之跨平台编译使用详解

    Golang交叉编译之跨平台编译使用详解

    这篇文章主要为大家介绍了Golang交叉编译之跨平台编译使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • golang基于errgroup实现并发调用的方法

    golang基于errgroup实现并发调用的方法

    这篇文章主要介绍了golang基于errgroup实现并发调用,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09
  • 深入浅析Go中三个点(...)用法

    深入浅析Go中三个点(...)用法

    这篇文章主要介绍了深入浅析Go中三个点(...)用法,需要的朋友可以参考下
    2021-10-10
  • golang将多路复异步io转成阻塞io的方法详解

    golang将多路复异步io转成阻塞io的方法详解

    常见的IO模型有阻塞、非阻塞、IO多路复用,异,下面这篇文章主要给大家介绍了关于golang将多路复异步io转成阻塞io的方法,文中给出了详细的示例代码,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
    2017-09-09
  • Go语言变量与基础数据类型详情

    Go语言变量与基础数据类型详情

    Go 是静态(编译型)语言,是区别于解释型语言的弱类型语言(静态:类型固定,强类型:不同类型不允许直接运算),下面文章将对其进行详细介绍,需要的朋友可以参考一下
    2021-09-09
  • 使用Go语言实现跨域资源共享(CORS)设置

    使用Go语言实现跨域资源共享(CORS)设置

    在Web开发中,跨域资源共享(CORS)是一种重要的安全机制,它允许许多资源在一个网页上被另一个来源的网页所访问,然而,出于安全考虑,浏览器默认禁止这种跨域访问,为了解决这个问题,我们可以使用Go语言来设置CORS,需要的朋友可以参考下
    2024-06-06
  • Go strconv包实现字符串和基本数据类型转换的实例详解

    Go strconv包实现字符串和基本数据类型转换的实例详解

    在Go语言(Golang)的编程实践中,strconv包是一个非常重要的标准库,它提供了在基本数据类型(如整型、浮点型、布尔型)和字符串之间的转换功能,本文给大家介绍了关于Go语言字符串转换strconv,需要的朋友可以参考下
    2024-09-09
  • go处理线程之间的交互示例代码

    go处理线程之间的交互示例代码

    Go语言以goroutine为核心实现并发编程,其中线程间交互主要通过Channels、WaitGroup、Mutex和Select实现,Channels提供goroutine间的数据传递,本文给大家介绍go处理线程之间的交互示例代码,感兴趣的朋友一起看看吧
    2024-10-10
  • 详解Go语言中select语句的常见用法

    详解Go语言中select语句的常见用法

    这篇文章主要是来和大家介绍一下Go语言中select 语句的常见用法,以及在使用过程中的注意事项,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2023-07-07
  • Go语言处理超大字符串型整数加减经典面试详解

    Go语言处理超大字符串型整数加减经典面试详解

    这篇文章主要为大家介绍了Go语言处理超大字符串型整数加减经典面试示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10

最新评论