并导入一个未选择模块的模块,但 go.sum 文件有 go.mod 文件哈希?

我现在正在使用 golang 1.13 并使用 go 模块。


但是,当我导入未在 go 模块中选择的包(例如,a)时,在 go.sum 文件中仍然有两行。Go 模块告诉我们“每个已知的模块版本都会在 go.sum 文件中产生两行。第一行给出模块版本文件树的哈希值。第二行将“/go.mod”附加到版本并给出仅模块版本的(可能是合成的)go.mod 文件的散列。go.mod-only 散列允许下载和验证模块版本的 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=

我的问题是,第二行是如何生成的?


qq_花开花谢_0
浏览 264回答 3
3回答

弑天下

go.sum包含特定模块版本内容的预期加密校验和。每次使用依赖项时,如果缺少或需要匹配 go.sum 中的现有条目,则将其校验和添加到 go.sum。每个包/模块都是依赖项,每个依赖项都意味着使用校验和进行维护,go.sum因此无论是包还是模块,它都会被维护。源将相应地下载到$GOPATH/src目录中。尝试 -

杨__羊羊

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

SMILET

或许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:定位到cmd/go/internal/modfetch/coderepo.go,给func添加断点legacyGoMod定位到cmd/go/internal/modfetch/coderepo_test.go,按 F5稍等片刻,停在断点处
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go