为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我使用 golang 模块,并导入一个未选择的模块,但 go.sum 文件具有 go.mod

为什么我使用 golang 模块,并导入一个未选择的模块,但 go.sum 文件具有 go.mod

Go
MMTTMM 2023-08-14 16:52:14
我现在使用 golang 1.13 并使用 go 模块。但是,当我导入一个未在 go 模块中选择的包(例如 a)时,在 go.sum 文件中仍然有两行。Go 模块告诉我们“每个已知的模块版本都会在 go.sum 文件中生成两行。第一行给出模块版本文件树的哈希值。第二行将“/go.mod”附加到版本并给出仅模块版本(可能是合成的)go.mod 文件的哈希值。仅 go.mod 哈希允许下载和验证模块版本的 go.mod 文件(计算依赖关系图所需),而无需下载所有模块的源代码代码。”(https://tip.golang.org/cmd/go/#hdr-Module_downloading_and_verification).但是这个包不是一个模块,所以它没有 go.mod 文件?例如,如果我导入不是模块的包调用“github.com/example/a”,在 go.sum 文件中,它仍然有这两行:github.com/example/a v0.0.0-20190627063042-31896c4e4162 h1:rSqi2vQEpS+GAFKrLvmxzWW3OGlLI4hANnEf/ib/ofo=github.com/example/a v0.0.0-20190627063042-31896c4e4162/go.mod h1:tcpxll8wcruwpPpWBbjAsWc1JbLHld/v9F+3rgLIr4c=我的问题是,第二行是如何生成的?
查看完整描述

3 回答

?
料青山看我应如是

TA贡献1772条经验 获得超7个赞

导致go.sum文件中写入每个依赖项总和哈希值。与您的文件相关的一个go.mod,以及从您已导入的模块导入的一个。尝试运行go mod tidy以减少导入的模块,您的go.mod文件将包含一些//indirect导入,即您导入的模块在内部使用的模块。



查看完整回答
反对 回复 2023-08-14
?
富国沪深

TA贡献1790条经验 获得超9个赞

go.sum包含特定模块版本内容的预期加密校验和。每次使用依赖项时,如果缺少或需要匹配 go.sum 中的现有条目,则将其校验和添加到 go.sum 中。

每个包/模块都是依赖项,每个依赖项都意味着用校验和来维护,go.sum因此无论是包还是模块,它都会被维护。

源文件将下载到$GOPATH/src相应的目录中。


查看完整回答
反对 回复 2023-08-14
?
眼眸繁星

TA贡献1873条经验 获得超9个赞

也许Golang源码可以解释原因:


func (r *codeRepo) legacyGoMod(rev, dir string) []byte {

    // We used to try to build a go.mod reflecting pre-existing

    // package management metadata files, but the conversion

    // was inherently imperfect (because those files don't have

    // exactly the same semantics as go.mod) and, when done

    // for dependencies in the middle of a build, impossible to

    // correct. So we stopped.

    // Return a fake go.mod that simply declares the module path.

    return []byte(fmt.Sprintf("module %s\n", modfile.AutoQuote(r.modPath)))

}

来自此处的代码: https: //github.com/golang/go/blob/acac535c3ca571beeb168c953d6d672f61387ef1/src/cmd/go/internal/modfetch/coderepo.go#L857

↓ VSCode 打开/usr/local/go/src

  1. 定位到cmd/go/internal/modfetch/coderepo.go,向 func 添加断点legacyGoMod

  2. 找到cmd/go/internal/modfetch/coderepo_test.go,按 F5

  3. 稍等片刻,停在断点处


查看完整回答
反对 回复 2023-08-14
  • 3 回答
  • 0 关注
  • 99 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信