DEV Community

Cover image for How to visualize dependencies in Golang
Paul
Paul

Posted on

How to visualize dependencies in Golang

#go

Background

I encountered an issue while building a golang project recently. Even though I specified the version of a dependency package, the version number was automatically upgraded during the build process. I didn't understand why at the time. Later, I learned about the go mod graph command, which lists all dependencies. I tried it, but it wasn't intuitive at all; I still had to copy the output into a text editor to search for information.

So, I searched online for related tools.

Other Tools

Search terms: Go Module dependency visualization

There are many tools available online. Most solutions use graphviz, echarts, or generate images. I tried a few.

graphviz is a powerful tool, but it requires separate installation. The generated SVG files aren't very readable either, especially when there are many dependent packages. For example, look at this one:

So, I turned to echarts.

Wow, it looks quite nice! The mouse hover feedback is good, and the color scheme is vibrant.

The echarts solution is beautiful, but not practical. With SVG I could at least search for package names; with this echart version, the package names aren't displayed.

My Solution

Alright, enough complaining. The problem still needs solving. Today, I'm introducing a visualization tool for go mod graphgmchart.

gmchart

github: https://github.com/PaulXu-cn/go-mod-graph-chart

Installation

go get -u github.com/PaulXu-cn/go-mod-graph-chart/gmchart
Enter fullscreen mode Exit fullscreen mode

Check if the installation was successful like this:

gmchart --help

Usage of ~\go\bin\gmchart:
  -debug int
        is debug model
  -keep int
        start http server not exit
Enter fullscreen mode Exit fullscreen mode

Usage

Navigate to your Golang project directory and run the command:

go mod graph | gmchart
Enter fullscreen mode Exit fullscreen mode

This should automatically open your browser. If it doesn't, open it manually.

Visit http://127.0.0.1:60306 to see the result.

As you can see, it renders the dependencies into a dependency tree. You can see which layer a particular package is introduced in, which is very intuitive. The webpage contains an SVG, making it easy to find a specific package—just use Ctrl+F to search.

Summary

After searching for so long, why wasn't there a suitable tool?

I thought about it. Visualization is a front-end specialty. Front-end developers don't typically use Golang. Those who do are rare, and among those, even fewer are interested in the go mod graph functionality. So, it fell to us back-end developers to create this tool.

Sigh~

Alternative Solutions

Top comments (0)