Go language print Call stack

Source: Internet
Author: User
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 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.