将go的结构定义转化为markdown 表格(帮助快速生产文档)

Type2md是一款用于将Golang结构定义转化成Markdown表格的工具,方便快速生成文档。它支持AST解析,结构体标签自定义,注释解析,默认值,必填项标记,以及对外部模块类型的引用解析。文章提供了安装、使用方法及示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Type2md - Golang Type To Markdown

github.com/eleztian/type2md

通过ast解析 将golang 的结构定义转化为markdown 表格。

支持将golang 的数据类型定义转化为markdown 表格

  • 支持自定义 struct tag
  • 支持注释解析
  • 支持默认值定义
  • 支持是否必填
  • 支持外部mod 类型引用解析。
  • 表格类型支持跳转

Usage

安装
$ go install github.com/eleztian/type2md
使用
$ type2md -h
Usage of type2md:
  -f string
        file path
  -t string
        file title
  -tag string
        struct tag name (default "json")
  -v    show version

Example

package test

import "github.com/eleztian/type2md/test/ext"

//go:generate type2md -f ../docs/doc_config.md github.com/eleztian/type2md/test Config

// Config doc.
type Config struct {
	Pre     ext.Hook
	Post    *ext.Hook
	Servers map[string]struct {
		Host string `json:"host,omitempty"`
		Port int    `json:"port" enums:"22,65522" require:"false"`
	} `json:"servers"` // server list
	InlineStruct `json:",inline"` // inline struct
	Slice        []string         // sss
	MapData      map[string]map[int]*OtherStruct
	ArrayData    [][2]string   `json:"array_data"`
	C            []interface{} // slice interface{}
}

// InlineStruct inline struct.
type InlineStruct struct {
	A string `json:"a"` // inline struct field a
}

// OtherStruct other struct
// this is use for test.
type OtherStruct struct {
	A string                 `json:"a" require:"true" default:"default value"`
	B [][2]ext.Mode          `json:"b"` // array string
	C map[string]interface{} `json:"c"` // map[string]interface{}
	D *OtherStruct           `json:"d"` // nested struct
}

执行下列命令

$ go generate  ./...          
2022/10/28 23:56:16 Current Module: github.com/eleztian/type2md
2022/10/28 23:56:16 start generate github.com/eleztian/type2md/test.Config
2022/10/28 23:56:16 start to save to ../docs/doc_config.md

得到下面的文档:


Config Doc

Config doc.

KeyTypeRequireDefaultDescribe
PreHooktrue
PostHooktrue
servers.{string}.hoststringfalse
servers.{string}.portintfalse- 22
- 65522
astringtrueinline struct field a.
Slice.[]stringtruesss.
MapData.{string}.{int}.OtherStructtrue
array_data.[] .[2]stringtrue
C.[]anytrueslice interface{}.
ext.Hook

Hook hook config.

KeyTypeRequireDefaultDescribe
namestringfalseexamplehook name.
commands.[]stringtruecommand list.
envs.{string}stringtrueenv key map.
modeModetrue1run mode.
OtherStruct

OtherStruct other struct
this is use for test.

KeyTypeRequireDefaultDescribe
astringtruedefault value
b.[] .[2]Modetruearray string.
c.{string}anytruemap[string] interface{}.
dOtherStructtruenested struct.
ext.Mode

Type: int
Mode mode define.

Enum ValueDescribe
1mode q.
2mode a.

github.com/eleztian/type2md/test.Config
GENERATED BY THE COMMAND type2md

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值