I am messing around creating modules and interlinking them.
When testing out behaviours I am calling packages that call other packages.
Is there an easy way to modify behaviour of fmt package with indentation level of future calls. So that parent can sets indentation level +1 before calling child package.
This way when each function prints out output I can easily see cascading dependencies in the stdout:
inside main
calling package X
____entering package X
____calling package Y
________package Y hello world
____leaving package X
back in main.
exiting
The
fmt
package does not support this out of the box.However, you can access the call depth with the help of
runtime.Callers()
, which means you don't even have to maintain the indentation "level" manually."Proof of concept"
Accessing the call depth is like this (which returns
0
formain()
,1
for a function called frommain()
etc.):And using it, an auto-indenter printing function:
Let's see it in action:
Which outputs (try it on the Go Playground):
Notes
I called the above solution "proof of concept" because it's not a solution handling all cases. You have to decide how you want to handle when new goroutines are launched. When new goroutines are launched, they are not called from
main()
, so the skip frames passed toruntime.Callers()
should be 1-less (runtime.Callers(5, pc)
instead ofruntime.Callers(6, pc)
). For how to detect this, see Check if function is being called as goroutine or not.