Capturing the stack trace of a panic
Printing or logging a stack trace when a panic is detected is a critical tool in identifying problems at runtime. This recipe shows how you can add a stack trace to your logging messages.
How to do it...
Use the debug.Stack
function with recover
:
import "runtime/debug" import "fmt" func main() { defer func() { if r := recover(); r != nil { stackTrace := string(debug.Stack()) // Work with stackTrace fmt.Println(stackTrace) } }() f() } func f() { var i *int *i=0 }
When inside the recovery function, the debug...