Go Module一篇就够

1.Module Path

  1. module path是module的唯一标识符.

  2. The main module is the module containing the directory where the go command is invoked.

  3. module的根目录就是go.mod文件所在的目录.包路径(package path)是module path和包所在子目录的连接=>所以module path是为该模块内的package path提供前缀。

    例如:

在这里插入图片描述

golang.org/x/net是module-path=>指向module的根目录,html是该根目录下的子目录=>所以import 该包时应该写成

import “golang.org/x/net/html”

  1. 如果module的主要版本V2及以上的,module path必须”/V2“这样的版本号结束这个V2可能是子目录名也可能不是子目录名

  2. [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L9MEVQo3-1654823605929)(Go Module详细解释.assets/image-20220608104257870-16546561795502.png)]

2.Versions

​ Module采用的语义版本(Semantic Version)

Semantic Version:

  1. 组成:a semantic version consists of three non-negative integers (the major, minor, and patch versions, from left to right) separated by dots.

  2. 含义:版本每个部分都表明了版本是否稳定且它是否与之前的版本兼容.

    Major Version:1. 递增的 2.当发生与之前版本不兼容的改变时(比如:包被删除、公共接口和文档被更改等),必须将Major Version递增且Minor Version、Patch Version设置为0。

    Minor Version:1 递增的 2.当发生与之前版本兼容的改变时(比如:添加一个新功能),则Minor Version递增且Patch Version设置为0。

    Patch Version: 1. 递增的 2.功能上没有与之前版本发生改变时(比如:debug bug 和优化),则Patch Version递增。

Major Version为0时或者有pre-release后缀时表示该版本不稳定且不需要满足兼容要求。

Pseudo-versions:

  1. 组成:Each pseudo-version has three parts.

  2. 含义:伪版本是一个未发行的版本.

3.如何解析import 包(如何确定哪个模块永远这个包)

如何找到module的?

  1. 前提:package path:golang.org/x/net/html;GOPROXY设置https://corp.example.com, https://proxy.golang.org

    一般设置代理为:go env -w GOPROXY=https://proxy.golang.com.cn,direct

    the go command(比如go mod tidy)将会做以下请求:

    当找到一个合适的module后,main module中go.mod文件中就会添加新的module的路径和版本号。 This ensures that when the same package is loaded in the future, the same module will be used at the same version. If the resolved package is not imported by a package in the main module, the new requirement will have an // indirect comment.

在找到module的情况下如何找到包的?

  1. import ”module-path/subdirectory-path“方式来定位包的位置.
  2. 以**example.com/a/b包路径(package path)**为例:
    1. example.com/a为module-path,example.com/a/b为package path
    2. 过程:the go command will check whether example.com/a contains the package, in the directory b. At least one file with the .go extension must be present in a directory for it to be considered a package.

3.Automatic updates

go mod tidy会执行automatic updates命令

4.Module-aware commands

大多数Go commands 将会在Module-aware或者GOPATH模式下运行。

Module-aware和GOPATH model区别与联系:

  1. module-aware:In module-aware mode, the go command uses go.mod files to find versioned dependencies, and it typically loads packages out of the module cache, downloading modules if they are missing.
  2. GOPATH model:In GOPATH mode, the go command ignores modules; it looks in vendor directories and in GOPATH to find dependencies.

Module-aware的控制:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存失败,源站可能有防盗链机制,建议将图片保存下来直接上传下上传(io5Ztzig9Wvu-1654823605931)(Go Module详细解释.assets/image-20220608130807606-165466488920610.png)(Go Module详细解释.assets/image-20220608130807606-165466488920610.png)]

在module-aware mode中,GOPATH仍然存储了下载的依赖( G O P A T H / p k g / m o d ∗ ∗ ) 和 安 装 的 命 令 ( ∗ ∗ GOPATH/pkg/mod**)和安装的命令(** GOPATH/pkg/mod)(GOPATH/bin)

Vendoring:

在module-aware机制下一般go mod tidy下载的包都存放在module-cache中即GOPATH/pkg/mod中,但是当在没有网络的情况下,如何保证项目的下载依赖包呢,所以需要创建一个vendor文件夹将项目所需要的依赖包放置到项目内部。

  1. go mod vendor 的作用:

    1. 创建名为vendor的文件夹
    2. 将该module的所有依赖包都放置在该vendor文件夹中(此处的依赖包是指非本模块外的所有依赖包无论该依赖包是否发布,不考虑build constratin约束之外的包)
    3. 在该vendor文件夹中创建modules.txt文件,表明vendor文件夹中包名+版本号
  2. 如果采用vendor机制开启,go command(比如:go buildandgo test)需要加载go包时会从vendor文件夹中加载而不是从module-cache中加载

    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.

    如何启用vendor机制?

    1. 如果go.mod文件中go的版本大于等于1.14且在该module的根目录下存在vendor文件夹则自动启用vendor机制

    2. 或者显式的使用在go command(比如:go buildandgo test)后面加上-mod=vendor(不启用vendor则用-mod=readonly 和 -mod=mod)

      含义:-mod=mod:表示忽略该模块根目录下的vendor文件夹会自动更新(上文提到的Automatic updates)go.mod,则使用的依赖包会先更新$GOPATH/pkg/mod下的依赖包然后使用该 文件夹下的依赖包

      ​ -mod=readonly:表示忽略该模块根目录下的vendor文件夹但不会自动更新go.mod且如果出现更新会报错

      ​ -mod=vendor:表示使用该模块根目录下的vendor文件夹中的依赖包,不会对vendor文件夹下的依赖包更新

  3. 如果在创建vendor/module.txt文件之后go.mod发生改变了,还要在运行一次go mod vendor,此时会先删除已经存在的vendor文件夹再重新构建vendor包

  4. go命令不检查vendor中的依赖库是否被修改,所以可以根据需要对vendor包中的包进行修改但是不建议这么做,go mod vendor则会对vendor文件夹下的依赖包检查是否修改因为当go mod vendor后会先删除已经存在的vendor文件夹再重新构建vendor包

术语:
在这里插入图片描述
**Module Cache:**一个本地的文件夹,里面存放的是下载的modules,located in GOPATH/pkg/mod.

go env -w GOPATH=//设置GOPATH路径

GOPATH下主要有三个文件夹pkg、src、bin

vendor directory: 一个名为vendor的文件夹,里面保存有需要构建的其他模块的包

pkg/mod.

go env -w GOPATH=//设置GOPATH路径

GOPATH下主要有三个文件夹pkg、src、bin

vendor directory: 一个名为vendor的文件夹,里面保存有需要构建的其他模块的包

猜你喜欢

转载自blog.csdn.net/Blockchain210/article/details/125215200