Go language print call stack
These two days to see Go code, er, too many, many Ctrip called a method, completely see not clear, so the thought is not the call stack to print out.
Check it out and find the Go Runtime/debug library can call the call stack. Let's look at an example:
Package main
Import (
"FMT"
"Runtime/debug"
)
func test1 () {
test2 ()
}
func test2 () {
test3 ()
}
func test3 () {
fmt. Printf ('%s ', Debug. Stack ())
Debug. Printstack ()
}
func main () {
test1 ()
}
As you can see from the code above, you can pass debug. Printstack () can be printed directly, or through Debug. The stack () method fetches and prints itself.
Run Tests
$ go run test_stacktrace.go goroutine 1 [running]: Runtime/debug. Stack (0x0, 0x0, 0x0)/usr/lib/golang/src/runtime/debug/stack.go:24 +0x80 main.test3 ()/tmp/test_stacktrace . go:17 +0x24 Main.test2 ()/tmp/test_stacktrace.go:13 +0x14 main.test1 ()/tmp/test_stacktrace.go:9 +0x14 ma In.main ()/tmp/test_stacktrace.go:22 +0x14 goroutine 1 [running]: Runtime/debug. Stack (0x0, 0x0, 0x0)/usr/lib/golang/src/runtime/debug/stack.go:24 +0x80 runtime/debug. Printstack ()/usr/lib/golang/src/runtime/debug/stack.go:16 +0x18 main.test3 ()/tmp/test_stacktrace.go:18 + 0x101 main.test2 ()/tmp/test_stacktrace.go:13 +0x14 main.test1 ()/tmp/test_stacktrace.go:9 +0x14 Main.main ()/tmp/test_stacktrace.go:22 +0x14