首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >GoPlantUML: 从Go代码到模型的逆向工程

GoPlantUML: 从Go代码到模型的逆向工程

作者头像
运维开发王义杰
发布2023-08-10 16:56:12
发布2023-08-10 16:56:12
1.1K00
代码可运行
举报
运行总次数:0
代码可运行

GoPlantUML是一个用于Go项目的PlantUML类图生成器。它可以生成与PlantUML兼容的类图文本,其中包含所有结构和接口的信息以及它们之间的关系。

安装

要使用GoPlantUML,你需要先安装Go语言,版本需要1.17或以上1。然后,你可以使用以下命令安装GoPlantUML:

代码语言:javascript
代码运行次数:0
运行
复制
go get github.com/jfeliu007/goplantuml/parser
go install github.com/jfeliu007/goplantuml/cmd/goplantuml@latest

用法

代码语言:javascript
代码运行次数:0
运行
复制
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
  -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
  -hide-private-members
        Hides all private members (fields and methods)

例子

在goplantuml中考虑了两种不同的关系:

  • 接口实现
  • 类型组成

下面的示例包含接口实现和组合。注意函数的签名方式

代码语言:javascript
代码运行次数:0
运行
复制
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() {

}

这将从前面的代码生成

代码语言:javascript
代码运行次数:0
运行
复制
@startuml
namespace testingsupport {
    interface MyInterface  {
        - foo() bool

    }
    class MyStruct1 << (S,Aquamarine) >> {
        - foo() bool

    }
    class MyStruct2 << (S,Aquamarine) >> {
    }
    class MyStruct3 << (S,Aquamarine) >> {
        - foo() 

        + Foo MyStruct1

    }
}
testingsupport.MyStruct1 *-- testingsupport.MyStruct2

testingsupport.MyInterface <|-- testingsupport.MyStruct1

testingsupport.MyStruct3 o-- testingsupport.MyStruct1

@enduml

模型效果

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-07-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 运维开发王义杰 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • GoPlantUML是一个用于Go项目的PlantUML类图生成器。它可以生成与PlantUML兼容的类图文本,其中包含所有结构和接口的信息以及它们之间的关系。
    • 安装
      • 用法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档