脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - Go语言中go mod vendor使用方法

Go语言中go mod vendor使用方法

2022-12-01 11:24test1280 Golang

go mod vendor的功能是将新增的依赖包自动写入当前项目的 vendor目录,下面这篇文章主要给大家介绍了关于Go语言中go mod vendor使用的相关资料,需要的朋友可以参考下

1.背景

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

Go语言中go mod vendor使用方法

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

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

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

PS:

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

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

2.环境

go环境:

D:workspacedemo>go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:Users梁翠翠AppDataLocalgo-build
set GOENV=C:Users梁翠翠AppDataRoaminggoenv
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:gopathpkgmod
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 FilesGo
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:Program FilesGopkg	oolwindows_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:workspacedemogo.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梁翠翠AppDataLocalTempgo-build648873300=/tmp/go-build -gno-record-gcc-switches

goland版本:

Go语言中go mod vendor使用方法

3.使用

示例:

Go语言中go mod vendor使用方法

项目demo由两个文件构成:

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

2.go.mod:记录当前项目demo依赖yaml module;

最常用、最简单的办法是,直接执行go mod vendor:

Go语言中go mod vendor使用方法

执行go mod vendor,将此项目依赖的gopkg.in/yaml.v2@v2.4.0下载到项目demo的根目录vendor中,并按照特定格式、规范组织。

如果此时你ctrl+鼠标点击import后面的yaml.v2时,将自动跳转到vendor目录下的yaml.v2:

Go语言中go mod vendor使用方法

而不再是GOPATH中的yaml.v2:

Go语言中go mod vendor使用方法

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

即使此刻,我们将GOPATH中的yaml.v2删除:

Go语言中go mod vendor使用方法

在项目中直接编译demo,不再需要下载yaml.v2依赖:

Go语言中go mod vendor使用方法

4.原理

官方文档请参见【重要!!!】:

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

2.https://golang.org/ref/mod#vendoring

命令行帮助:

D:workspacedemo>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使用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文地址:https://blog.csdn.net/test1280/article/details/120855865

延伸 · 阅读

精彩推荐
  • GolangGo 自定义error错误的处理方法

    Go 自定义error错误的处理方法

    这篇文章主要介绍了Go 自定义error错误的处理方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...

    周伯通的麦田12802021-03-26
  • Golanggolang json数组拼接的实例

    golang json数组拼接的实例

    这篇文章主要介绍了golang json数组拼接的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    pingd4152021-06-05
  • Golang解决Golang并发工具Singleflight的问题

    解决Golang并发工具Singleflight的问题

    前段时间在一个项目里使用到了分布式锁进行共享资源的访问限制,后来了解到Golang里还能够使用singleflight对共享资源的访问做限制,于是利用空余时间了...

    北戴河游泳5042022-10-07
  • GolangGolang实现Directional Channel(定向通道)

    Golang实现Directional Channel(定向通道)

    这篇文章主要介绍了Golang实现Directional Channel(定向通道),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    L1ng147292021-03-28
  • Golang关于go-zero单体服务使用泛型简化注册Handler路由的问题

    关于go-zero单体服务使用泛型简化注册Handler路由的问题

    这篇文章主要介绍了go-zero单体服务使用泛型简化注册Handler路由,涉及到Golang环境安装及配置Go Module的相关知识,本文给大家介绍的非常详细,对大家的学习...

    修车课代表11792022-07-27
  • Golanggolang并发工具MapReduce降低服务响应时间

    golang并发工具MapReduce降低服务响应时间

    这篇文章主要为大家介绍了golang并发使用MapReduce降低服务响应时间实践使用示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早...

    zhoushuguang5412022-09-29
  • GolangGo语言二维数组的传参方式

    Go语言二维数组的传参方式

    这篇文章主要介绍了Go语言二维数组的传参方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    hello_bravo_4212021-06-04
  • GolangGo语言基础模板设计模式示例详解

    Go语言基础模板设计模式示例详解

    这篇文章主要为大家介绍了Go语言基础设计模式之模板模式的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步...

    package main8432021-12-06