Go调用链可视化工具使用实例探究

 更新时间:2024年01月18日 10:02:17   作者:机器铃砍菜刀  
本文介绍一款工具 go-callvis,它能够将 Go 代码的调用关系可视化出来,并提供了可交互式的 web 服务,在接手他人代码或调研一些开源项目时,如果能够理清其中的代码调用链路,这将加速我们对实现的理解

go-callvis 使用

依赖

  • Go 1.17+
  • Graphviz (可选,当工具指定了 -graphviz 时需要)

工具安装

go get -u github.com/ofabry/go-callvis
# or
git clone https://github.com/ofabry/go-callvis.git
cd go-callvis && make install

示例

package main
import (
func main() {
 // Part 1: create a listener
 l, err := net.Listen("tcp", ":8000")
 if err != nil {
  log.Fatalf("Error listener returned: %s", err)
 }
 defer l.Close()
 for {
  // Part 2: accept new connection
  c, err := l.Accept()
  if err != nil {
   log.Fatalf("Error to accept new connection: %s", err)
  }
  // Part 3: create a goroutine that reads and write back data
  go func() {
   log.Printf("TCP session open")
   defer c.Close()
   for {
    d := make([]byte, 1024)
    // Read from TCP buffer
    _, err := c.Read(d)
    if err != nil {
     log.Printf("Error reading TCP session: %s", err)
     break
    }
    log.Printf("reading data from client: %s\n", string(d))
    // write back data to TCP client
    _, err = c.Write(d)
    if err != nil {
     log.Printf("Error writing TCP session: %s", err)
     break
    }
   }
  }()
 }
}

以上是一个简单的TCP服务端代码,通过 go-callvis 工具,可将其代码调用关系梳理出来。

$ go-callvis main.go
2022/08/14 21:23:03 http serving at http://localhost:7878
2022/08/14 21:23:03 converting dot to svg..
2022/08/14 21:23:03 serving file: /var/folders/xk/gn46n46d503dsztbc6_9qb2h0000gn/T/go-callvis_export.svg

go-callvis 默认将代码调用关系存储成 svg 格式的图形,并会在 http://localhost:7878 服务上进行展示。

在浏览器界面上,如果点击 log 单元,将会进入 log 模块的代码调用交互图中。

使用参数

go-callvis 默认以 main 作为链路起点进行分析,因此 package 需要为 main 包。

go-callvis [flags] package

如果不想从 main 方法开始,那么需要使用 -tests 参数,并且在 yourpackage 下创建单元测试,在测试中调用你想要的起始点方法。

go-callvis -tests yourpackage

详细使用说明可通过执行 go-callvis 命令查看

$ go-callvis
go-callvis: visualize call graph of a Go program.
Usage:
  go-callvis [flags] package
  Package should be main package, otherwise -tests flag must be used.
Flags:
  -algo string
     The algorithm used to construct the call graph. Possible values inlcude: "static", "cha", "rta", "pointer" (default "pointer")
  -cacheDir string
     Enable caching to avoid unnecessary re-rendering, you can force rendering by adding 'refresh=true' to the URL query or emptying the cache directory
  -debug
     Enable verbose log.
  -file string
     output filename - omit to use server mode
  -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)
  -nodeshape string
     graph node shape (see graphvis manpage for valid values) (default "box")
  -nodestyle string
     graph node style (see graphvis manpage for valid values) (default "filled,rounded")
  -nointer
     Omit calls to unexported functions.
  -nostd
     Omit calls to/from packages in standard library.
  -rankdir string
     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.
  -version
     Show version and exit.
[slp@slpdeMacBook-Pro:] ~/repo/MongoShake/cmd/collector $ go-callvis --help
Usage of go-callvis:
  -algo string
     The algorithm used to construct the call graph. Possible values inlcude: "static", "cha", "rta", "pointer" (default "pointer")
  -cacheDir string
     Enable caching to avoid unnecessary re-rendering, you can force rendering by adding 'refresh=true' to the URL query or emptying the cache directory
  -debug
     Enable verbose log.
  -file string
     output filename - omit to use server mode
  -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)
  -nodeshape string
     graph node shape (see graphvis manpage for valid values) (default "box")
  -nodestyle string
     graph node style (see graphvis manpage for valid values) (default "filled,rounded")
  -nointer
     Omit calls to unexported functions.
  -nostd
     Omit calls to/from packages in standard library.
  -rankdir string
     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.
  -version
     Show version and exit.

每个参数都有对应的说明,无需详细介绍。

有几个比较有用的参数可以注意:nostd用以忽略标准库的调用;group用以对函数分类;includelimitignore参数则用以控制过滤或保留调用关系。

总结

go-callvis 工具将 Go 程序函数调用关系通过图形可视化出来,它能帮助开发人员更好地梳理程序脉络。且 go-callvis 的使用非常简单,可以开箱即用。

之后同学们在接触复杂项目时,不妨用 go-callvis 试试看。

以上就是Go调用链可视化工具使用实例探究的详细内容,更多关于Go调用链可视化的资料请关注脚本之家其它相关文章!

相关文章

  • Go语言快速入门图文教程

    Go语言快速入门图文教程

    Go是 Goolge 开发的一种静态型、编译型、并发型,并具有垃圾回收功能的语言,Go 语言上手非常容易,它的风格类似于 C 语言,Go 语言号称是互联网时代的 C 语言,那么它到底有多火呢,一起看看吧
    2021-05-05
  • Go语言的缓存策略与实现方法代码示例

    Go语言的缓存策略与实现方法代码示例

    在Go语言中缓存是一种常见的优化技术,用于减少重复计算或I/O操作,提高程序性能,这篇文章主要介绍了Go语言缓存策略与实现方法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2026-04-04
  • Go基础教程之正则表达式regexp库示例详解

    Go基础教程之正则表达式regexp库示例详解

    Go语言中的正则表达式功能强大,但也具有一定的复杂性,理解和掌握其基本语法和用法能够帮助我们在开发中更高效地处理文本数据,这篇文章主要介绍了Go基础教程之正则表达式regexp库的相关资料,需要的朋友可以参考下
    2025-10-10
  • Go1.18新特性工作区模糊测试及泛型的使用详解

    Go1.18新特性工作区模糊测试及泛型的使用详解

    这篇文章主要为大家介绍了Go 1.18新特性中的工作区 模糊测试 泛型使用进行详细讲解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • 详解Golang中的Mutex并发原语

    详解Golang中的Mutex并发原语

    Mutex 是 Go 语言中互斥锁的实现,它是一种同步机制,用于控制多个 goroutine 之间的并发访问。本文将着重介绍 Go 的 Mutex 并发原语,希望对大家有所帮助
    2023-03-03
  • go语言的初始化顺序,包,变量,init详解

    go语言的初始化顺序,包,变量,init详解

    这篇文章主要介绍了go语言的初始化顺序,包,变量,init详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • Go  iota 常量基本语法介绍

    Go  iota 常量基本语法介绍

    这篇文章主要介绍了Go 为什么要设计 iota 常量,我们介绍了 Go 中 iota 的基本语法。同时基于历史资料针对 iota 到底是什么,为什么要这么叫,又有什么用进行了一番研究,需要的朋友可以参考下
    2022-06-06
  • Go语言获取本机逻辑CPU数量的方法

    Go语言获取本机逻辑CPU数量的方法

    这篇文章主要介绍了Go语言获取本机逻辑CPU数量的方法,实例分析了runtime库的操作技巧,需要的朋友可以参考下
    2015-03-03
  • go值赋值和引用赋值的使用

    go值赋值和引用赋值的使用

    本文将介绍Go语言中的值赋值和引用赋值,并比较它们之间的差异,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-10-10
  • Go语言并发编程之控制并发数量实现实例

    Go语言并发编程之控制并发数量实现实例

    这篇文章主要为大家介绍了Go语言并发编程之控制并发数量实例探究,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01

最新评论