杜龙少(sdvdxl)

golang打印错误栈信息

Word count: 65 / Reading time: 1 min
2016/03/15 Share
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package mainimport (
"runtime"
"fmt")func main() {
outer()}func outer() {
inner()}func inner() {

defer func() {
if err := recover(); err != nil {
trace := make([]byte, 1024)
count := runtime.Stack(trace, true)
fmt.Printf("Recover from panic: %s\n", err)
fmt.Printf("Stack of %d bytes: %s\n", count, trace)
}
}()

panic("Fake error!")}
CATALOG