查找Golang中每个依赖项的校验和。

huangapple go评论76阅读模式
英文:

Find checksum of every dependency in golang

问题

我想要能够获取由Go程序使用的每个包的校验和,包括在模块内使用的包。

标准库中的runtime/debug包中有一个ReadBuildInfo()函数,非常好用,但它只提供模块的数据,而不是包的数据。

示例代码如下:

package pkgA

var Foo = 1
package pkgB

import "pkgA"

var Bar = pkgA.Foo
package main

import (
	"fmt"
	"runtime/debug"

	"example/pkgB"
)

func main() {
	_ = pkgB.Bar

	b, ok := debug.ReadBuildInfo()
	if !ok {
		fmt.Println("not ok!")
		return
	}

	for _, module := range b.Deps {
		fmt.Println(module.Path, module.Sum)
	}
}

输出结果如下:

pkgB v0.0.0-20210225235400-92e28d816f64

这里没有关于pkgA的信息。我认为这是因为pkgBpkgA都属于同一个模块。

问题:有没有办法访问pkgA的校验和?

英文:

I want to be able to get the checksum of every package used by a go program, including packages used within modules.

runtime/debug in the standard library has ReadBuildInfo(), which is great, but it only gives data for modules, not for packages.

Example:

package pkgA

var Foo = 1
package pkgB

import "pkgA"

var Bar = pkgA.Foo
package main

import (
	"fmt"
	"runtime/debug"

	"example/pkgB"
)

func main() {
	_ = pkgB.Bar

	b, ok := debug.ReadBuildInfo()
	if !ok {
		fmt.Println("not ok!")
		return
	}

	for _, module := range b.Deps {
		fmt.Println(module.Path, module.Sum)
	}
}

The output is like

pkgB v0.0.0-20210225235400-92e28d816f64

There is no info on A. I believe this is because pkgB and pkgA both belong to the same module.

Question: Is there any way to access the checksum for pkgA?

答案1

得分: 1

Go校验和数据库存储的是模块的校验和,而不是包的校验和。
嵌入在二进制文件中的调试信息不包括从包到模块的映射关系,但如果你可以访问模块的源代码,你可以使用go list命令报告从包到模块的映射关系:

go list -f '{{if .Module}}{{.ImportPath}}: {{.Module}}{{end}}' all

你可以使用这个映射关系和模块级别的校验和来验证每个包是否具有正确的源代码。(注意,go mod verify已经实现了这个验证功能。)

英文:

The Go checksum database stores checksums for modules, not packages.
The debug information embedded in a binary does not include the mapping from packages to modules, but if you have access to the module's source you can use go list to report the mapping from packages to modules:

go list -f '{{if .Module}}{{.ImportPath}}: {{.Module}}{{end}}' all

You can use that mapping, in conjunction with the module-level checksums, to verify that each package has the correct source code. (Note that go mod verify already implements that verification.)

huangapple
  • 本文由 发表于 2021年10月19日 02:11:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/69620678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定