Similar to the question: How to dump goroutine stacktraces?
I took over a go program but it's fairly complex and I am not able to manually follow the code flow by reading code as some methods are executed and I have no idea why.
Is there a way I could print out all the methods that were executed after main.go was run?
I am aware of: https://golang.org/pkg/net/http/pprof/ but I am able to see only goroutines run, not the specific methods I am unable to backtrace/reverse engineer.
When I'm not sure how a method got called, I find the easiest is to debug.PrintStack()
at the start of it. If that scrolls past too fast, make it panic
.
I think if you printed all the methods executed, the signal would get lost in the noise.
Another helpful way is to write a unit test that just calls a part of the code you are interested in. Run it and watch what happens. Put some print statements in. Once you figure out what's happening, add some checks to your unit test (to make it a real test), and you've improved the code already.
Once you have a unit test (or any entry point), you can also step into it with delve
.
Michael Feather's "Working Effectively With Legacy Code" is pretty good, it gives lots of strategies to attack legacy code, and refactor it to goodness.
Finally, I've found it's difficult to make Go really confusing, so I'm usually really glad if the complex code I inherit is in Go :-)