英文:
How to get all dependencies (modules) used?
问题
我尝试获取我项目使用的所有依赖项。
我查看了go.mod
文件,但它只包含我添加的依赖项/模块,而不包括我的依赖项的依赖项。
查看go.sum
文件,这看起来更有希望,但我注意到其中包含了多个重复项,尽管我只使用了一个版本的依赖项。例如:
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
如何获取仅包含所有活跃使用的依赖项/模块的列表?
英文:
I try to get all the dependencies my project uses.
I looked at the go.mod
file, though this only contains dependencies/modules that I added, and not the dependencies from my dependencies.
Looking at the go.sum
file, this looked more promising, though then I noticed it contains multiple duplicates. Even though I only use one version of the dependency. As an example:
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
How can I get a list containing only all actively used dependencies/modules?
答案1
得分: 1
你对这两个文件的理解是正确的,从技术上讲,你可以通过在go.mod文件中添加replace语句来清理go.sum文件。不过,不能保证你(和你的团队)始终保持这种清理。
在我看来,获取你所需列表的最佳方式是使用go list -m all
命令(或者使用go list --json -m all
以获取JSON响应)。它会递归列出项目中的所有依赖项,但会过滤掉未使用的依赖项。
英文:
You are right about these 2 files, and technically you can clean up the go.sum by adding replace statements to the go.mod file. Though, it is not reliable that you (and your team) will always keep this clean.
In my opinion, the best way to get the list you are looking for is go list -m all
(or go list --json -m all
to get a json response). It will list you all the dependencies recursively in your project. But filters out the unused ones.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论