Print names of functions from runtime.Stack

I am trying to find a way where I can print the names of the function that are present in the stack trace of all the current goroutines.

https://play.golang.org/p/VnLpAAZb_vm

In the above example, I am able to find that main.main() calls main.mine(), but how can I extract the name of that, and also how can I get to know that the main.another() function is also invoked?

I think what you’re looking for is https://golang.org/pkg/runtime/#Callers which lets you build up a []uintptr slice of the function pointers in your call stack. Here’s an example: https://github.com/skillian/expr/blob/505e7bc29386b34d243efe6d7cf0d8146c76dc6c/errors/pc.go#L33

After that, you then want to use https://golang.org/pkg/runtime/#CallersFrames like this: https://github.com/skillian/expr/blob/505e7bc29386b34d243efe6d7cf0d8146c76dc6c/errors/pc.go#L47

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.