异常捕获 发表于 2019-12-18 | 分类于 go 字数统计: | 阅读时长 ≈ 123456789101112131415161718192021func PanicTrace(kb int) []byte { s := []byte("/src/runtime/panic.go") e := []byte("\ngoroutine ") line := []byte("\n") stack := make([]byte, kb<<10) //4KB length := runtime.Stack(stack, true) start := bytes.Index(stack, s) stack = stack[start:length] start = bytes.Index(stack, line) + 1 stack = stack[start:] end := bytes.LastIndex(stack, line) if end != -1 { stack = stack[:end] } end = bytes.Index(stack, e) if end != -1 { stack = stack[:end] } stack = bytes.TrimRight(stack, "\n") return stack}