Visualise Call graph of a Go Program using go-callvis

Checking the call graph (function's call) of a program is very useful to understand the workflow and logic of a project. For a big project like docker or Kubernetes its very hard to understand the code by seeing its source code.

Go-callvis is open source tool by go developer community to generate the call graph based on the package and its method calling. Its very handy to understand and also generate pretty good graph in our web-browser. we can also generate graph in our local file system based on the parameter, below is the picture where we can see that how go-callvis generate graph.


image source from github: https://github.com/ofabry/go-callvis

Above is the graph generate by go-callvis, where we can see all the node and its workflow.

Based on this in this tutorial i am going to use a simple project linklist and generate graph using go-callvis and demonstrate how to use it in our project.

I am going to explain it step by step so you can also follow it and generate graph for your project, let us know in comment if you get any trouble while generating the graph.

Step 1: we need to install the go-callvis in our workspace for this,

go get -u github.com/ofabry/go-callvis


Step 2. Check go-callvis is installed in your local workspace by using below command: 

./go-callvis -version 

Step 3. If its not running, in that case: 


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

 ./go-callvis -version

 Step 4. Now all our set up is done, we are going to call our main function with go-callvis, for this i am going to use below code snippet


Above code is for linklist where we can check our go-callvis project, to run the code, Please clone it on your local machine.

After clonning copy the go-callvis bin to same file and run the below command:

 ./go-callvis ll.go

You will able to see below output to terminal:

2021/08/20 16:15:35 http serving at http://localhost:7878
2021/08/20 16:15:35 converting dot to svg..
2021/08/20 16:15:35 serving file: /tmp/go-callvis_export.svg
Opening in existing browser session.
 
Open the url http://localhost:7878, you will able to see below graph in your browser:


Above is the call graph for linklist function same method you can apply on any project you want.

Some key point before using go-callvis, if you wanted to target any specific package try to use: -ignore and -limit flag with go-calvis for ignoring unnecessary package,

for example: if in my current project i do not want to track the fmt package in that case i use below command:

./go-callvis -ignore fmt ll.go

Using above query my new output is:

You can see on above picture its ignore the fmt package.

Use this tools in you project and let us know if you have any query regarding this, we will help you in set up this tools in your local machine. 

Check our other post for MCQ quizzes on various topic.

Thank you

Post a Comment

0 Comments