golang:程序崩溃时堆栈跟踪单个例程

I am just curious to know if there is a way to get my go application to only output the stack trace for the routine which panics (and subsequently dies) rather than all of my goroutines as there are quite a number of them.

I would assume that there is some form of flag which I can pass to go run or go build to do this but cannot seem to find it.

Any help is appreciated.

I would perhaps try (not tested) using runtime.Stack in a defered function while modifying (for debug purposes) your existing code for debug purposes:

const debug = true //TODO turn off for production

func MyPotentiallyPanickingGoroutine() {
        if debug {
            buf := make([]byte, 1<<16)
            defer func() {
                    fmt.Printf("%s
", runtime.Stack(buf, false))
            }()
        }

        // existing code follows
}