Is there a way to show the dependency graph of a Go package?
Asked Answered
S

2

16

For example, given a package A that depends on package B and package C, where package C also depends on package D - is there a way to output this information? (Using a vendoring tool or otherwise)

The vendor.yaml output by govend doesn't include transitive dependency information - neither does the Gopkg.toml file output by dep, from what I can see. The go.mod file produced by Golang 1.11's mod and does annotate some dependencies as // indirect - but it does not annotate dependencies with any information about which dependency they were pulled in via.

Sheet answered 7/9, 2018 at 15:28 Comment(1)
Depending on exactly what you want, go mod graph or go mod why (see go mod help graph and go mod help why) may be of use. The former can be piped through digraph to answer some more specific questions about the graph. It would also be fairly easy to filter the go mod graph output into a form that could be piped into dot to produce an image.Wahoo
I
17

Did you try https://github.com/KyleBanks/depth? It does provide a decent dependency tree at first look I tried.

Illsuited answered 7/9, 2018 at 15:42 Comment(0)
S
1

Nowadays the best way to figure dependency graph is to use go mod graph.

Here is the usage explanation from Go 1.21 (from 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'.

Here is the example output:

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]
Spagyric answered 27/1, 2024 at 20:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.