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
}

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

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 Build Status Mentioned in Awesome Go DUMELS Diagram

GoPlantUML

PlantUML Class Diagram Generator for golang projects. Generates class diagram text compatible with plantuml with the information of all structures and interfaces as well as the relationship among them.

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.10 or above

Installing

go get github.com/jfeliu007/goplantuml/parser
go get github.com/jfeliu007/goplantuml/cmd/goplantuml
cd $GOPATH/src/github.com/jfeliu007/goplantuml
go install ./

This will install the command goplantuml in your GOPATH bin folder.

Usage

goplantuml [-recursive] path/to/gofiles path/to/gofiles2
goplantuml [-recursive] path/to/gofiles path/to/gofiles2 > diagram_file_name.puml
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

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)