golang可视化Go程序调用图插件库go-callvis的使用

Golang可视化Go程序调用图插件库go-callvis的使用

简介

go-callvis是一个开发工具,用于通过交互式视图可视化Go程序的调用图。这个工具的目的是为开发者提供Go程序的视觉概述,使用来自调用图的数据及其与包和类型的关系。这在代码复杂度较高的大型项目中特别有用,或者当你只是想理解别人的代码时。

gopher

特性

  • 点击包可以快速切换焦点(使用交互式查看器)
  • 聚焦程序中的特定包
  • 按包分组函数
  • 按接收者类型分组方法
  • 过滤特定导入路径前缀的包
  • 忽略标准库的调用
  • 省略各种类型的函数调用

输出预览

main

查看上面图片对应的源代码

工作原理

它运行指针分析来构建程序的调用图,并使用数据生成dot格式的输出,可以用Graphviz工具渲染。

快速开始

安装

要求:

  • Go 1.19+
  • Graphviz (可选,仅在使用-graphviz标志时需要)

安装go-callvis:

# 最新发布版本
go install github.com/ofabry/go-callvis@latest

# 开发版本
go install github.com/ofabry/go-callvis@master

或者克隆仓库并编译源代码:

# 克隆仓库
git clone https://github.com/ofabry/go-callvis.git
cd go-callvis

# 编译并安装
make install

使用

交互式查看器

要使用由提供聚焦包SVG图像的网络服务器提供的交互式视图,可以简单地运行:

go-callvis <target package>

HTTP服务器默认监听http://localhost:7878/,使用选项-http="ADDR:PORT"可以更改HTTP服务器地址。

渲染静态输出

要生成单个输出文件,使用选项-file=<file path>选择输出文件目的地。

输出格式默认为svg,使用选项-format=<svg|png|jpg|…>选择不同的输出格式。

选项

Usage of go-callvis:
  -debug
    	Enable verbose log.
  -file string
    	output filename - omit to use server mode
  -cacheDir string
    	Enable caching to avoid unnecessary re-rendering.
  -focus string
    	Focus specific package using name or import path. (default "main")
  -format string
    	output file format [svg | png | jpg | ...] (default "svg")
  -graphviz
    	Use Graphviz's dot program to render images.
  -group string
    	Grouping functions by packages and/or types [pkg, type] (separated by comma) (default "pkg")
  -http string
    	HTTP service address. (default ":7878")
  -ignore string
    	Ignore package paths containing given prefixes (separated by comma)
  -include string
    	Include package paths with given prefixes (separated by comma)
  -limit string
    	Limit package paths to given prefixes (separated by comma)
  -minlen uint
    	Minimum edge length (for wider output). (default 2)
  -nodesep float
    	Minimum space between two adjacent nodes in the same rank (for taller output). (default 0.35)
  -nointer
    	Omit calls to unexported functions.
  -nostd
    	Omit calls to/from packages in standard library.
  -rankdir
        Direction of graph layout [LR | RL | TB | BT] (default "LR")
  -skipbrowser
    	Skip opening browser.
  -tags build tags
    	a list of build tags to consider satisfied during the build. For more information about build tags, see the description of build constraints in the documentation for the go/build package
  -tests
    	Include test code.
  -algo string
        Use specific algorithm for package analyzer: static, cha or rta (default "static")
  -version
    	Show version and exit.

运行go-callvis -h列出所有支持的选项。

示例代码

下面是一个使用go-callvis的简单示例程序:

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
    foo()
    bar()
}

func foo() {
    fmt.Println("foo called")
}

func bar() {
    fmt.Println("bar called")
    foo()
}

要可视化这个程序的调用图,可以运行:

go-callvis -focus=main -file=callgraph.svg .

这将生成一个SVG文件,显示main包中所有函数的调用关系。

参考指南

包/类型

代表 样式
focused 蓝色
stdlib 绿色
other 黄色

函数/方法

代表 样式
exported 粗体边框
unexported 普通边框
anonymous 点状边框

调用

代表 样式
internal 黑色
external 棕色
static 实线
dynamic 虚线
regular 简单箭头
concurrent 带圆圈的箭头
deferred 带菱形的箭头

更多示例

这里是项目syncthing的一个示例。

syncthing example

查看更多示例和使用的命令选项。


更多关于golang可视化Go程序调用图插件库go-callvis的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang可视化Go程序调用图插件库go-callvis的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


go-callvis:Go程序调用图可视化工具

go-callvis 是一个用于可视化 Go 程序调用关系的工具,它能生成程序的静态调用图,帮助开发者理解代码结构和调用关系。

安装

go install github.com/ofabry/go-callvis@latest

基本使用

1. 简单示例

go-callvis -focus=main -group=pkg,type ./main.go

这会生成一个以 main 函数为中心的调用图,按包和类型分组。

2. 常用参数

  • -focus: 指定关注的包或函数
  • -group: 分组方式 (pkg, type)
  • -ignore: 忽略的包
  • -file: 输出文件名
  • -format: 输出格式 (png, svg, dot)

3. 完整示例

go-callvis \
    -focus=github.com/your/project \
    -group=pkg,type \
    -ignore=github.com/your/project/vendor \
    -file=callgraph \
    -format=png \
    ./...

代码示例

假设我们有以下 Go 程序:

// main.go
package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano())
	result := processData(10)
	fmt.Println("Result:", result)
}

func processData(n int) int {
	sum := 0
	for i := 0; i < n; i++ {
		sum += generateNumber()
	}
	return sum
}

func generateNumber() int {
	return rand.Intn(100)
}

要生成这个程序的调用图:

go-callvis -focus=main -group=pkg,type ./main.go

高级用法

1. 作为库使用

你也可以将 go-callvis 作为库集成到你的程序中:

package main

import (
	"os"

	"github.com/ofabry/go-callvis/callvis"
)

func main() {
	args := &callvis.AnalyzerArgs{
		FocusPkg:  "main",
		GroupBy:   []string{"pkg", "type"},
		Ignore:    []string{"vendor"},
		OutputFormat: "png",
		OutputFile:   "callgraph",
	}

	err := callvis.Render(args, "./...")
	if err != nil {
		panic(err)
	}
}

2. 生成交互式网页

go-callvis 还支持生成交互式网页:

go-callvis -focus=main -http=:8080 ./...

然后在浏览器中访问 http://localhost:8080

常见问题

  1. 依赖问题:确保你的项目已经 go mod tidy
  2. 大型项目:对于大型项目,可以使用 -ignore 参数忽略不关心的包
  3. 输出格式:SVG 格式通常更适合复杂调用图

替代方案

如果你需要更强大的可视化工具,可以考虑:

  1. go-callgraph:生成调用图数据
  2. Delve:调试器,可以生成运行时调用图
  3. pprof:性能分析工具,包含调用图功能

go-callvis 特别适合快速了解代码结构和调用关系,是代码审查和架构分析的有力工具。

回到顶部