Go: Stack traces

A stack trace is the list of functions that the program was in the middle of when the stack trace was printed.

Stack traces are typically printed to the console when unexpected error occurs. They can be very useful for debugging: - not only do you see where the error happened, - but also how the program arrived in this place.


goroutine 11 [running]:
        /usr/local/go/src/testing/testing.go:711 +0x2d2
panic(0x53f820, 0x594da0)
        /usr/local/go/src/runtime/panic.go:491 +0x283
github.com/yourbasic/bit.(*Set).Max(0xc42000a940, 0x0)
        ../src/github.com/bit/set_math_bits.go:137 +0x89
        ../src/github.com/bit/set_test.go:165 +0x337
testing.tRunner(0xc420092690, 0x57f5e8)
        /usr/local/go/src/testing/testing.go:746 +0xd0
created by testing.(*T).Run
        /usr/local/go/src/testing/testing.go:789 +0x2de

The stack trace can be read from the bottom up:

The indented lines show the source file and line number at which the function was called. The hexadecimal numbers refer to parameter values, including values of pointers and internal data structures. Stack Traces in Go has more details.

Print a stack trace

To print the stack trace for the current goroutine, use debug.PrintStack from package runtime/debug.

You can also examine the current stack trace programatically by calling runtime.Stack.

Level of detail

The GOTRACEBACK variable controls the amount of output generated when a Go program fails.


Be the first to comment!