聊聊gost的GoSafely
序 本文主要研究一下gost的GoSafely GoSafely gost/runtime/goroutine.go func GoSafely(wg *sync.WaitGroup, ignoreRecover bool, handler func(), catchFunc func(r interface{})) { if wg != nil { wg.Add(1) } go func() { defer func() { //...... }() handler() }() } GoSafely接收WaitGroup、ignoreRecover、handler、catchFunc参数,其大致的模板是,首先对WaitGroup进行add(1),然后一步执行带defer的handler defer gost/runtime/goroutine.go defer func() { if r := recover(); r != nil { if !ignoreRecover { fmt.Fprintf(os.Stderr, "%s goroutine panic: %v\n%s\n", time.Now(), r, string(debug.Stack())) } if catchFunc != nil { //...... } } if wg != nil { wg.Done()