modgraphviz converts "go mod graph" output into Graphviz's DOT language, enabling visualization and analysis of Go module dependency graphs. It serves as a bridge between Go's module system and the Graphviz visualization toolkit.
go install golang.org/x/exp/cmd/modgraphviz@latestmodgraphviz is a command-line tool that transforms module dependency data from Go's module system into the DOT language format used by Graphviz. This enables you to create visual representations of your Go project's module dependency graph, making it easier to understand and analyze complex dependency relationships.
The tool automatically distinguishes between selected and non-selected module versions by coloring them differently:
The simplest way to use modgraphviz is to pipe the output of go mod graph directly to it:
go mod graph | modgraphviz > graph.dotThe tool reads the graph format generated by go mod graph on standard input and writes DOT language output to standard output. It requires no options or arguments.
modgraphviz operates as a pipeline tool with a single responsibility: convert Go module graph format to Graphviz DOT format.
Input: "go mod graph" output (module dependency edges)
Output: DOT language format (Graphviz-compatible graph definition)Transforms Go module dependency information into Graphviz's DOT language format for visualization.
go mod graph | modgraphviz > graph.dotDescription: Reads module dependencies from standard input and outputs DOT format to standard output. No command-line options or arguments are supported.
Pipe the DOT output directly to the dot command to generate image files.
go mod graph | modgraphviz | dot -Tpng -o graph.pngDescription: Creates a PNG image of the dependency graph. The -Tpng option specifies PNG format, and -o graph.png sets the output filename. Other Graphviz output formats (svg, pdf, ps) can be used by changing the -T option.
Generate scalable vector graphics for web-friendly dependency visualization.
go mod graph | modgraphviz | dot -Tsvg -o graph.svgDescription: Creates an SVG image with selectable text and scalable quality. SVG is ideal for web pages and documents requiring zoom capability.
Convert your Go module graph to DOT format for inspection or further processing:
cd /path/to/your/go/project
go mod graph | modgraphviz > dependencies.dot
cat dependencies.dotOutput format:
digraph {
"github.com/user/moduleA" [color=green];
"github.com/user/moduleB" [color=green];
"github.com/user/moduleC" [color=grey];
"github.com/user/moduleA" -> "github.com/user/moduleB";
"github.com/user/moduleB" -> "github.com/user/moduleC";
}Create a PNG visualization of your module dependencies:
go mod graph | modgraphviz | dot -Tpng -o dependencies.pngThe resulting PNG shows:
Use Graphviz's interactive viewer for real-time exploration:
go mod graph | modgraphviz | dottyThis opens an interactive window where you can navigate the dependency graph, zoom, pan, and inspect individual nodes.
Find strongly connected components (circular dependencies) in your module graph:
go mod graph | modgraphviz | sccmapThis identifies any circular dependency patterns that might need resolution.
Generate multiple visualization formats in sequence:
go mod graph | modgraphviz > deps.dot
# PNG for presentations
dot -Tpng -o deps.png deps.dot
# SVG for web
dot -Tsvg -o deps.svg deps.dot
# PDF for documentation
dot -Tpdf -o deps.pdf deps.dotFor large projects, save the intermediate DOT file for repeated analysis:
# Generate DOT once
go mod graph | modgraphviz > project_deps.dot
# Experiment with different layouts
dot -Tpng -Kcirco -o circular_layout.png project_deps.dot
dot -Tpng -Kfdp -o organic_layout.png project_deps.dot
dot -Tpng -Kdot -o hierarchical_layout.png project_deps.dotGo uses the Minimal Version Selection algorithm to choose which versions of dependencies to include in builds. modgraphviz visualizes this by:
This helps identify which versions are actually used versus which are available alternatives.
The input format is produced by go mod graph and contains module paths and versions:
module@version dependency@versionFor example:
github.com/user/myapp@v1.0.0 github.com/user/dep@v1.2.0
github.com/user/dep@v1.2.0 github.com/other/lib@v2.0.0modgraphviz converts this into DOT format with proper node and edge definitions.
The DOT language is Graphviz's native format for describing graphs. It specifies:
Example DOT output:
digraph {
"github.com/example/mod@v1.0.0" [color=green];
"github.com/example/mod@v1.0.0" -> "github.com/dependency/mod@v2.0.0";
}modgraphviz is most useful when combined with other tools:
dot, dotty, sccmap, and others for visualization and analysisWhen investigating how your modules depend on each other:
go mod graph | modgraphviz | dot -Tpng -o deps.png
# Open deps.png in your image viewerTo find and visualize circular dependencies:
go mod graph | modgraphviz | sccmap | dot -Tpng -o circles.pngInclude dependency graphs in your project documentation:
go mod graph | modgraphviz > docs/dependencies.dot
dot -Tsvg -o docs/dependencies.svg docs/dependencies.dotmodgraphviz has no command-line options or arguments. Configuration is entirely through piping and Graphviz's own options on subsequent tools. All behavior is determined by the input from go mod graph and the Graphviz tools used for visualization.
go mod graph commandGraphviz supports multiple layout algorithms. Try different ones with your DOT file:
dot -Kneato -Tpng -o layout1.png deps.dot # Spring model
dot -Kfdp -Tpng -o layout2.png deps.dot # Force-directed placement
dot -Kcirco -Tpng -o layout3.png deps.dot # Circular layout
dot -Ktwopi -Tpng -o layout4.png deps.dot # Radial layoutFor projects with many dependencies, filter the graph before visualization:
# Save DOT file
go mod graph | modgraphviz > all_deps.dot
# Filter nodes manually in the DOT file or use grep
grep "specific-module" all_deps.dot > filtered_deps.dotModify the DOT output for custom styling:
go mod graph | modgraphviz | \
sed 's/\[color=grey\]/[color=lightgrey, style=dashed]/' | \
dot -Tpng -o styled_deps.png