Golang 可視化工具之go-callvis的實現
一 背景
日常我們會看一些開源項目,或者想看自己寫的代碼可視化,在golang中可以利用工具實現,go-callvis是以圖片的形式展示了go程序的調用關系,在看復雜項目時尤其有用。
二 go-callvies 簡介
2.1 工具簡介
Go-callvis 是一個開發(fā)工具,用于幫助使用交互式視圖可視化 Go 程序的調用圖。
此工具的目的是為開發(fā)人員提供一個可視化的 Go 程序概覽,使用來自調用圖的數據及其與包和類型的關系。這在大型項目中特別有用,因為這些項目的代碼復雜度要高得多,或者只是簡單地試圖理解其他人的代碼。
2.2 特性
- 支持go mod
- 程序中的特定焦點包
- 點擊軟件包,使用交互式查看器快速切換焦點
- 按類型按包和/或方法分組函數
- 過濾器包到特定的導入路徑前綴
- 忽略標準庫中的函數
- 省略各種類型的函數調用
三 安裝
- go get安裝
$ go get -u github.com/ofabry/go-callvis
- 源碼編譯安裝
$ git clone https://github.com/ofabry/go-callvis.git cd go-callvis && make install
四 輸出示例

它運行指針分析來構造程序的調用圖,并使用數據生成點格式的輸出,可以使用 Graphviz 工具進行渲染。
五 安裝
go get -u github.com/ofabry/go-callvis # or git clone https://github.com/ofabry/go-callvis.git cd go-callvis && make install
六 使用
go-callvis [可選參數] <包路徑>
1)使用瀏覽器交互展示
默認情況下,HTTP服務器在http://localhost:7878/上偵聽,請使用選項`-http="ADDR:PORT"`更改HTTP服務器地址。
2)靜態(tài)輸出
要生成單個輸出文件,使用選項-file=<file path>選擇輸出文件目標。
輸出格式默認為svg,使用選項-format=<svg|png|jpg|…>選擇其他輸出格式。
3)其他參數
-debug
輸出詳細日志
-file string
以文件新式輸出結果,并將忽略server交互模式
-focus string
Focus specific package using name or import path. (default "main")
-format string
輸出文件的格式 [svg | png | jpg | ...] (默認 "svg")
-graphviz
使用Graphviz渲染圖像
-group string
分組功能,根據包 與/或 類型分組 [pkg, type] (用逗號分開) (默認 "pkg"),例如 -group pkg,type
-http string
HTTP 服務地址 (默認 ":7878")
-ignore string
忽略包含給定前綴的包路徑(用逗號分開)
-include string
包含具有給定前綴的包路徑 (用逗號分開)
-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
忽略對未導出函數的調用。
-nostd
忽略標準庫的調用
-skipbrowser
跳過打開瀏覽器
-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
包含測試代碼
-version
顯示版本號
1)解析main包
go-callvis -skipbrowser -nostd ./
2)解析testPkg包
go-callvis -group pkg,type -focus testPkg github.com/項目具體路徑 簡單示例
$ go-callvis -format png -group pkg,type -focus github.com/kaliarch/cobra /cmd/app github.com/kaliarch/cobra 2021/08/18 11:22:12 http serving at http://localhost:7878 2021/08/18 11:22:21 converting dot to png.. 2021/08/18 11:22:22 serving file: /var/folders/wn/367g1v9n1bv0sg1k8qldzym80000gn/T/go-callvis_export.png

到此這篇關于Golang 可視化工具之go-callvis的實現的文章就介紹到這了,更多相關Golang go-callvis內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在VS?Code中配置Go開發(fā)環(huán)境的完整過程
如果您希望在本地計算機上開始使用Go語言進行開發(fā),但尚未配置好VSCode中的Go支持,則可能是由于缺少必要工具鏈、擴展或環(huán)境變量設置,可以看看這篇文章,這篇文章主要介紹了在VS Code中配置Go開發(fā)環(huán)境的完整過程,需要的朋友可以參考下2026-04-04

