Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
256 lines (180 sloc) 7.29 KB

Visually analysing module dependencies

go mod graph prints the module requirement graph in a simple text form. In this guide we explore a couple of different techniques to visualise this data.

We use the module as an example because it has a large number of dependencies which makes for nice visualisations.


Checkout the Buffalo module:

$ cd /home/gopher/scratchpad
$ git clone --depth=1 --branch v0.13.0
Cloning into 'buffalo'...
$ cd buffalo

Ensure all dependencies of the Buffalo module are available locally:

$ go mod download
go: finding v0.0.3
go: finding v0.0.0-20180802152300-74f5a47f4d56
go: finding v0.0.0-20181005191442-d6ee392ec72e
go: finding v1.0.1
go: finding v1.2.1
go: finding v1.1.3
go: finding v4.8.4+incompatible

Examine the help for go mod graph to understand the output format:

$ go help mod graph
usage: go mod graph

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.

Look at a sample of that output:

$ go mod graph

The output from go mod graph contains requirements as listed in modules' go.mod files. Hence we might see multiple requirements at different minor/patch versions for the same major version of a module. This gets resolved to a single major version per module during the build phase, but for this analysis we de-duplicate the requirement graph by module major version:

$ go mod graph | sed -Ee 's/@[^[:blank:]]+//g' | sort | uniq >unver.txt

This results in:

$ cat unver.txt

Visualise the resulting module dependency graph as a directed graph using Graphviz's dot command:

$ echo "digraph {" >
$ echo "graph [rankdir=TB, overlap=false];" >>
$ cat unver.txt | awk '{print "\""$1"\" -> \""$2"\""};' >>
$ echo "}" >>
$ twopi -Tsvg -o dag.svg

This results in:

Module Dependency Graph

Convert the module requirement graph into a histogram of module dependencies:

$ cat unver.txt | awk '{print $1}' | sort | uniq -c | sort -nr | awk '{print $2 "\t" $1}' >hist.txt

This results in:

$ cat hist.txt	60	27	23	22	18	16	16

Using, generate a radial chart from the histogram data:

$ dchart -psize=10 -pwidth=30 -left=50 -top=50 -radial -textsize=1.5 -chartitle=Buffalo hist.txt >radial.xml
$ svgdeck -outdir radial -pagesize 1000,1000 radial.xml

This results in:

Dependency Histogram Radial

Generate a horizontal bar chart from the histogram data:

$ dchart -hbar -left=40 -top=90 -textsize=1.1 -chartitle=Buffalo hist.txt >hbar.xml
$ svgdeck -outdir hbar -pagesize 1000,1000 hbar.xml

This results in:

Dependency Histogram Horizontal Bar

Version details

go version go1.12.5 linux/amd64 commit 7b4a8a7c9dfb9243ab16d8a2abd1cedb553e4094
You can’t perform that action at this time.