iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💮

Generating PlantUML Class Diagrams from Go Packages

に公開

This time, just a quick tip.

Have you ever thought it would be great to represent packages made in Go with UML diagrams? It seems many people feel the same way, as various tools have been released. Among them, I would like to introduce jfeliu007/goplantuml today.

goplantuml consists of a parser for analyzing Go packages and a CLI (Command-Line Interface) tool that uses it. There also seems to be a web service called Dumels that utilizes this package. I'll try out the CLI tool here.

Downloading and Building the CLI Tool

Since binaries don't seem to be provided, download and build it using the go get command. If module mode is on, the following should work fine.

$ go get github.com/jfeliu007/goplantuml/cmd/goplantuml@latest

$ goplantuml -h
Usage of goplantuml:
  -aggregate-private-members
    	Show aggregations for private members. Ignored if -show-aggregations is not used.
  -hide-connections
    	hides all connections in the diagram
  -hide-fields
    	hides fields
  -hide-methods
    	hides methods
  -ignore string
    	comma separated list of folders to ignore
  -notes string
    	Comma separated list of notes to be added to the diagram
  -output string
    	output file path. If omitted, then this will default to standard output
  -recursive
    	walk all directories recursively
  -show-aggregations
    	renders public aggregations even when -hide-connections is used (do not render by default)
  -show-aliases
    	Shows aliases even when -hide-connections is used
  -show-compositions
    	Shows compositions even when -hide-connections is used
  -show-connection-labels
    	Shows labels in the connections to identify the connections types (e.g. extends, implements, aggregates, alias of
  -show-implementations
    	Shows implementations even when -hide-connections is used
  -show-options-as-note
    	Show a note in the diagram with the none evident options ran with this CLI
  -title string
    	Title of the generated diagram

Looking good.

Analyzing Packages

To perform analysis, just pass the directory containing the package as an argument.

$ goplantuml ~/go/src/github.com/spiegel-im-spiegel/pa-api > pa-api.puml

The analysis results are output to the standard output in PlantUML description format, so redirect it appropriately. Then, simply use PlantUML to convert it into image data.

java -jar /path/to/plantuml.jar -charset UTF-8 pa-api.puml

The results look like this.

pa-api.png

You can see it is properly organized by package. Note that adding the -recursive option will recursively analyze packages in subdirectories. While multiplicity is usually essential when drawing UML diagrams, since we are generating diagrams from code here, it might not be that critical.

Use it as a companion for your documentation.

Note on the dot command in Windows

When using the dot command included in Graphviz 2.44 for Windows[1], it seems necessary to descend to the folder containing the dot.exe command in the command prompt and run the dot -c command beforehand.

Bonus

You can visualize module-level dependencies using my tool spiegel-im-spiegel/depm. For example, it looks like this.

depm modules

For more details, please refer to the introduction page below.

That’s all for the advertisement (lol).

References

脚注
  1. PlantUML uses the dot command from Graphviz for rendering. ↩︎

GitHubで編集を提案

Discussion