登录
首页 >  Golang >  Go问答

有没有办法显示 Go 包的依赖关系图?

来源:stackoverflow

时间:2024-04-07 19:09:34 211浏览 收藏

编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《有没有办法显示 Go 包的依赖关系图?》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。

问题内容

例如,给定一个包 A 依赖于包 B 和包 C,其中包 C 也依赖于包 D - 有没有办法输出此信息? (使用供应商工具或其他方式)

govend 输出的 vendor.yaml 不包含传递依赖信息 - Gopkg.toml 文件也不包含 据我所知,dep 的输出。 go.mod 文件由 Golang 1.11 的 mod 生成,并将某些依赖项注释为 //indirect - 但它不会使用有关它们通过哪个依赖项拉入的任何信息来注释依赖项。


解决方案


您尝试过https://github.com/KyleBanks/depth吗? 乍一看我尝试过,它确实提供了一个不错的依赖关系树。

现在计算依赖关系图的最佳方法是使用 go mod graph

以下是 go 1.21 的使用说明(来自 go help mod graph):

usage: go mod graph [-go=version] [-x]

graph prints the module requirement graph (with replacements applied)
in text form. each line in the output has two space-separated fields: a module
and one of its requirements. each module is identified as a string of the form
path@version, except for the main module, which has no @version suffix.

the -go flag causes graph to report the module graph as loaded by the
given go version, instead of the version indicated by the 'go' directive
in the go.mod file.

the -x flag causes graph to print the commands graph executes.

see https://golang.org/ref/mod#go-mod-graph for more about 'go mod graph'.

这是示例输出:

example.com/main example.com/[email protected]
example.com/main example.com/[email protected]
example.com/[email protected] example.com/[email protected]
example.com/[email protected] example.com/[email protected]
example.com/[email protected] example.com/[email protected]
example.com/[email protected] example.com/[email protected]

终于介绍完啦!小伙伴们,这篇关于《有没有办法显示 Go 包的依赖关系图?》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>