pprof

embedded-struct-visualizer

该工具通过扫描 Go 代码所在目录,然后将 Go 代码层次结构可视化。这对于复杂项目的数据结构或识别特定结构的依赖关系非常有用。

使用方法

安装 embedded-struct-visualizer

$ go install github.com/davidschlachter/embedded-struct-visualizer@latest

我默认大家已经安装好 go 环境了 😁

embedded-struct-visualizer[1] 的用法:

执行下面的命令:

$ embedded-struct-visualizer -out demo.gv .
Usage: embedded-struct-visualizer [OPTIONS] DirToScan
If the directory to scan is not provided, it defaults to './'
OPTIONS:
  -out <file>  path to output file (default: write to stdout)
  -v           verbose logging

实操

main.go
package main

import (
 "time"
 "domain.tld/user"
)

type A struct {
 B
 C map[string]D
}

type B struct {
 E, F  string
 G     user.Status
 Timer H
}

type D struct {
 I uint64
}

type H struct {
 Timer time.Ticker
 J     chan D
}
embedded-struct-visualizer
digraph {
"main.A" -> { "main.D" "main.B" };
"main.B" -> { "user.Status" "main.H" };
"main.H" -> { "main.D" "time.Ticker" };
}

这个看起来并不美观,那只是我们打开的姿势不对。这里需要用到 graphviz 。

brew install graphviz

其他环境,可参考官网安装:https://graphviz.org/download/[2]

dot -Tps fancy.gv -o fancy.ps

如果你还想对层次图上的对象做一些美化,那你可以再了解一下 graphviz guide[3]。

fancy.ps
digraph G {
 size ="4,4";
 main [shape=box]; /* this is a comment */
 main -> parse [weight=8];
 parse -> execute;
 main -> init [style=dotted];
 main -> cleanup;
 execute -> { make_string; printf}
 init -> make_string;
 edge [color=red]; // so is this
 main -> printf [style=bold,label="100 times"];
 make_string [label="make a\nstring"];
 node [shape=box,style=filled,color=".7 .3 1.0"];
 execute -> compare;
}
dot -Tps fancy.gv -o fancy.ps
图片

📢 注: 如果你指定的字体找不到,可能会报以下错误:Converting the PostScript file “fancy.ps” produced the following warnings: · Handlee not found, using Courier. 其他 graphviz example https://sketchviz.com/graphviz-examples

引用链接

[1] embedded-struct-visualizer: https://github.com/davidschlachter/embedded-struct-visualizer

[2] https://graphviz.org/download/: https://graphviz.org/download/

[3] graphviz guide: https://www.graphviz.org/pdf/dotguide.pdf

- END -

扫码关注公众号「网管叨bi叨」

给网管个星标,第一时间吸我的知识 👆

网管整理了一本《Go 开发参考书》收集了70多条开发实践。去公众号回复【gocookbook】领取!还有一本《k8s 入门实践》讲解了常用软件在K8s上的部署过程,公众号回复【k8s】即可领取!

觉得有用就点个在看  👇👇👇