dsj2222222 2018-03-22 19:58
浏览 263

如何打印在golang程序中执行的所有方法

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.

  • 写回答

1条回答 默认 最新

  • doucang9673 2018-03-24 00:33
    关注

    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 :-)

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)