Skip to content

Instantly share code, notes, and snippets.

@carlo-colombo
Created February 18, 2020 10:04
Show Gist options
  • Select an option

  • Save carlo-colombo/ddccc39ef31f2afb1aff496417abab9f to your computer and use it in GitHub Desktop.

Select an option

Save carlo-colombo/ddccc39ef31f2afb1aff496417abab9f to your computer and use it in GitHub Desktop.
Generating go projects packages graph in DOT language (Graphviz)
#!/bin/bash
set -e
root_package="$(awk '/module/ {print $2}' go.mod)"
go list -f '{{.ImportPath}} {{range .Imports}}{{.}} {{end}}' ./... \
| awk -v root_package="${root_package}/" '
BEGIN {
FS=" ";
printf "digraph \"%s\" {\n", root_package
}
{
gsub(root_package, "", $1)
for(i=2; i<=NF; i++) {
if (index($i, root_package) != 0) {
gsub(root_package, "", $i)
printf "\t\"%s\"->\"%s\"\n",$1, $i;
}
}
}
END{
print "}"
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment