GoPlantUML,go代码到类图使用 GoPlantUML,开发人员可以毫不费力地可视化其 Go 项目中的结构和关系
前言
GoPlantUML 是一个开源工具,旨在简化从 Go 源代码生成 PlantUML 图的过程。使用 GoPlantUML,开发人员可以毫不费力地可视化其 Go 项目中的结构和关系,从而有助于代码理解和文档编写。通过解析 Go 源代码并生成 PlantUML 图,GoPlantUML 使开发人员能够为其代码库架构、包依赖关系和函数交互创建清晰简洁的可视化表示。该工具通过提供复杂 Go 项目的可视化概览,简化了文档编制过程并增强了团队成员之间的协作。
安装
先决条件: Golang 1.17 或更高版本
go get github.com/jfeliu007/goplantuml/parser
go install github.com/jfeliu007/goplantuml/cmd/goplantuml@latest
如果没有PlantUML插件,最好也安装一个,可以直接展示生成的内容。 也可以在线预览 www.plantuml.com/plantuml/um…
用法
goplantuml [-recursive] 路径1 路径2 > diagram_file_name.puml
Usage of goplantuml:
-aggregate-private-members : 显示私有成员的聚合。如果未使用-show- aggreations,则忽略。
-hide-connections : 隐藏关系图中的所有连接
-hide-fields : 隐藏字段
-hide-methods : 隐藏方法
-ignore string : 要忽略的文件夹列表,以逗号分隔
-notes string : 要添加到图表中的注释列表,以逗号分隔
-output string : 输出文件路径。如果省略,则默认为标准输出
-recursive : 递归遍历所有目录
-show-aggregations : 即使使用-hide-connections也会呈现公共聚合(默认情况下不呈现)
-show-aliases : 即使使用-hide-connections也会呈现公共聚合(默认情况下不呈现)
-show-compositions : 即使使用-hide-connections也显示组合
-show-connection-labels : 在连接中显示标签以标识连接类型(例如扩展、实现、聚合、别名)
-show-implementations : 即使使用-hide-connections也显示实现
-show-options-as-note : 在使用该CLI运行的无明显选项的图中显示注释
-title string : 生成图的标题
-hide-private-members : 隐藏所有私有成员(字段和方法)
demo
package testingsupport
//MyInterface only has one method, notice the signature return value
type MyInterface interface {
foo() bool
}
//MyStruct1 will implement the foo() bool function so it will have an "extends" association with MyInterface
type MyStruct1 struct {
}
func (s1 *MyStruct1) foo() bool {
return true
}
//MyStruct2 will be directly composed of MyStruct1 so it will have a composition relationship with it
type MyStruct2 struct {
MyStruct1
}
//MyStruct3 will have a foo() function but the return value is not a bool, so it will not have any relationship with MyInterface
type MyStruct3 struct {
Foo MyStruct1
}
func (s3 *MyStruct3) foo() {
}
执行生成uml
goplantuml ./ > test.puml
模型效果
转载自:https://juejin.cn/post/7414733004885606435