DEV Community

Cover image for How to turn your golang programs into UML Class diagrams
jfeliu007
jfeliu007

Posted on

How to turn your golang programs into UML Class diagrams

Sometimes it helps to see a class diagram of your projects to understand the big picture. I have always been a fan of PlantUML. When it comes to drawing UML diagrams, plantUML is one of my favorites since drawings are generated out of a human-readable text file, which can be easily committed to git.

For that reason, I decided to write a Golang library to generate class diagrams from a Golang project.

Goplantuml is a Golang library with an added command that you can install if you run go already. It basically turns code like this



package goisawesome

import "strings"

type Foo struct {
    field       int
    PublicField bool
    *strings.Builder
}
type MyInterface interface {
    GetValue() bool
}

func (f *Foo) myPrivateFunction() int {
    return 3
}

func (f *Foo) GetValue() bool {
    return true
}

func (f *Foo) MyPublicFunction() bool {
    return true
}


Enter fullscreen mode Exit fullscreen mode

into this



@startuml
namespace goisawesome {
    class Foo {
        - field int
        + PublicField bool
        - myPrivateFunction() 
        + GetValue() 
        + MyPublicFunction() 
    }
    interface MyInterface {
        + GetValue() 
    }
}
strings.Builder *-- goisawesome.Foo
goisawesome.MyInterface <|-- goisawesome.Foo
@enduml


Enter fullscreen mode Exit fullscreen mode

which can be used with plantuml to generate a nice class diagram.

see example here

If you are interested, swing by the goplantuml repo.

GitHub logo jfeliu007 / goplantuml

PlantUML Class Diagram Generator for golang projects

godoc reference Go Report Card codecov License: MIT GitHub release Mentioned in Awesome Go DUMELS Diagram

GoPlantUML V2

GoPlantUML is an open-source tool developed to streamline the process of generating PlantUML diagrams from Go source code. With GoPlantUML, developers can effortlessly visualize the structure and relationships within their Go projects, aiding in code comprehension and documentation. By parsing Go source code and producing PlantUML diagrams, GoPlantUML empowers developers to create clear and concise visual representations of their codebase architecture, package dependencies, and function interactions. This tool simplifies the documentation process and enhances collaboration among team members by providing a visual overview of complex Go projects. GoPlantUML is actively maintained and welcomes contributions from the Go community.

Want to try it on your code?

Take a look at www.dumels.com. We have created dumels using this library.

Code of Conduct

Please, review the code of conduct here.

Prerequisites

golang 1.17 or above

Installing

go get github.com/jfeliu007/goplantuml/parser
go install github.com/jfeliu007/goplantuml/cmd/goplantuml@latest

This will install the command goplantuml in…

I would love to hear you opinions. I also wrote an article about the program and how it is used if you would like more information.

Top comments (0)