Stacktrace of goroutine when enclosing function ends



func main() {
    timeout := time.NewTimer(n)
    go longRunningFn()

Is it possible to get a stacktrace of longRunningFn when main times out?


Yes, you can use the runtime.Stack to print the stack of all goroutines:

Stack formats a stack trace of the calling goroutine into buf and returns the number of bytes written to buf. If all is true, Stack formats stack traces of all other goroutines into buf after the trace for the current goroutine.

Simple GoPlay here:

It also looks like you can print out the stack of a specific goroutine by using debug.PrintStack in conjunction with the runtime library. Credit from another S.O. answer here: How to dump goroutine stacktraces?

Answered By – william.taylor.09

Answer Checked By – Mary Flores (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.