【问题标题】:How to determine version of a golang module through its source code?如何通过源代码确定 golang 模块的版本?
【发布时间】:2021-01-21 12:40:27
【问题描述】:

最近,我正在尝试确定 golang 模块的版本。通常,我会下载一个模块的源代码,知道主模块的版本。但有时其他目录中的子模块很少。与github.com/hashicorp/consul 一样,它包含两个子模块hashicorp/consul/sdk 和hashicorp/consul/api。但是,当我下载 github.com/hashicorp/consul(版本:v1.9.1)时,很难确定 consul/sdk 和 consul/api 的版本,因为 go.mod 文件不包含有关此模块的任何版本信息。那么,我的问题是如何在 golang 模块中获取子模块的版本?

我读了checksum documentsendpoints documents。我想知道是否可以计算源文件的校验和并将其与 golang 数据库中的版本校验和进行比较以确定版本。当我阅读端点 api 时,我认为这种方式有点复杂。

另一个想法是通过 git tag history 来确定版本。但是,我认为这不是一个准确的方法。

【问题讨论】:

  • 你去.mod 包含版本。

标签: go go-modules


【解决方案1】:

如果您查看 github.com/hashicorp/consul 的 go.mod 文件,您可以看到版本:

    github.com/hashicorp/consul/api v1.8.0
    github.com/hashicorp/consul/sdk v0.7.0

(参见例如https://github.com/hashicorp/consul/blob/master/go.mod#L31

根据您使用的版本,检查该版本的相应go.mod 文件。

正如@Volker 所说,您的go.mod 文件可能也有版本。如果不是,您的 go.sum 文件将包含所有依赖项的校验和。

【讨论】:

  • 这可能是一个解决方案。但是,如果你把这个 repo 的标签切换到api/v1.8.1,你会发现go.mod 中“github.com/hashicorp/consul/sdk”的版本仍然是v1.8.0,而不是v1.8.1。这表明该方法不准确。
  • 当然,您可以在您的存储库中手动升级到更新版本的 sdk,而不是使用 consul 团队为该版本的 consul 准备的版本。但是OP想知道什么版本的sdk属于consul v1.9.1。这回答了这个问题。
【解决方案2】:

要确定所有版本的依赖项,请尝试使用go list 工具 (List packages or modules)

例如(所有依赖版本的列表)

% cat go.mod
module github.com/hashicorp/consul

go 1.13

replace github.com/hashicorp/consul/api => ./api ?? replace ➡️ consul/api v1.8.0

replace github.com/hashicorp/consul/sdk => ./sdk ?? replace ➡️ consul/sdk v0.7.0

replace launchpad.net/gocheck => github.com/go-check/check v0.0.0-20140225173054-eb6ee6f84d0a

require (
    github.com/Microsoft/go-winio v0.4.3 // indirect
    github.com/NYTimes/gziphandler v1.0.1
    github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e
    github.com/armon/go-metrics v0.3.6
    github.com/armon/go-radix v1.0.0
    github.com/aws/aws-sdk-go v1.25.41
    github.com/coredns/coredns v1.1.2
    github.com/coreos/go-oidc v2.1.0+incompatible
    github.com/digitalocean/godo v1.10.0 // indirect
    github.com/docker/go-connections v0.3.0
    github.com/elazarl/go-bindata-assetfs v0.0.0-20160803192304-e1a2a7ec64b0
    github.com/envoyproxy/go-control-plane v0.9.5
    github.com/frankban/quicktest v1.11.0 // indirect
    github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d
    github.com/golang/protobuf v1.3.5
    github.com/google/go-cmp v0.5.2
    github.com/google/go-querystring v1.0.0 // indirect
    github.com/google/gofuzz v1.2.0
    github.com/google/tcpproxy v0.0.0-20180808230851-dfa16c61dad2
    github.com/hashicorp/consul/api v1.8.0 ?? replace
    github.com/hashicorp/consul/sdk v0.7.0 ?? replace
...
% go list -json -m -u all
{
        "Path": "github.com/hashicorp/consul",
        "Main": true,
        "Dir": "/Users/a18388871/GoProjects/consul",
        "GoMod": "/Users/a18388871/GoProjects/consul/go.mod",
        "GoVersion": "1.13"
}

...

{
        "Path": "github.com/grpc-ecosystem/grpc-gateway",
        "Version": "v1.9.0",
        "Time": "2019-05-14T09:07:28Z",
        "Update": {
                "Path": "github.com/grpc-ecosystem/grpc-gateway",
                "Version": "v1.16.0",
                "Time": "2020-10-28T10:29:51Z"
        },
        "Indirect": true,
        "Dir": "/Users/a18388871/go/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.9.0",
        "GoMod": "/Users/a18388871/go/pkg/mod/cache/download/github.com/grpc-ecosystem/grpc-gateway/@v/v1.9.0.mod"
}
{
        "Path": "github.com/hashicorp/consul/api",
        "Version": "v1.8.0",
        "Replace": { ??. Replace field describes the replacement module, and its Dir field is set to the replacement's source code
                "Path": "./api",
                "Dir": "/Users/a18388871/GoProjects/consul/api",
                "GoMod": "/Users/a18388871/GoProjects/consul/api/go.mod",
                "GoVersion": "1.12"
        },
        "Update": { ??' The -u flag adds information about available upgrades'
                "Path": "github.com/hashicorp/consul/api",
                "Version": "v1.8.1",
                "Time": "2020-12-10T20:49:49Z"
        },
        "Dir": "/Users/a18388871/GoProjects/consul/api",
        "GoMod": "/Users/a18388871/GoProjects/consul/api/go.mod",
        "GoVersion": "1.12"
}


...

【讨论】:

  • 这可能不是很准确,因为最新版本的 consul/api 是v1.8.1。在目录中使用命令时,我仍然得到 v1.8.0。更重要的是,有时主模块不使用子模块作为其依赖项。比如,github.com/google/cadvisor 及其子模块github.com/google/cadvisor/cmd
  • 这是因为在go.mod 中使用了replace github.com/hashicorp/consul/api => ./api 指令。输出中的Replace 字段显示依赖项已被替换(在本例中,来自 v1.8.1 版本的本地项目包),但еру Version 字段仍显示来自require 块的go.mod 的版本
猜你喜欢
  • 2012-01-30
  • 2020-09-12
  • 2017-01-21
  • 2011-04-01
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 2021-06-05
相关资源
最近更新 更多