When the program is processed in multiple goroutines, panic may occur, which will cause main goroutine to crash directly. Can following logic add to Group.Go() ?
// Go calls the given function in a new goroutine.
//
// If the function returns an error it is added to the group multierror which
// is returned by Wait.
func (g *Group) Go(f func() error) {
g.wg.Add(1)
go func() {
defer g.wg.Done()
+ defer func() {
+ if r := recover(); r != nil {
+ g.mutex.Lock()
+ g.err = Append(g.err, fmt.Errorf("%v", r))
+ g.mutex.Unlock()
+ }
+ }()
if err := f(); err != nil {
g.mutex.Lock()
g.err = Append(g.err, err)
g.mutex.Unlock()
}
}()
}
When the program is processed in multiple goroutines, panic may occur, which will cause main goroutine to crash directly. Can following logic add to Group.Go() ?
// Go calls the given function in a new goroutine. // // If the function returns an error it is added to the group multierror which // is returned by Wait. func (g *Group) Go(f func() error) { g.wg.Add(1) go func() { defer g.wg.Done() + defer func() { + if r := recover(); r != nil { + g.mutex.Lock() + g.err = Append(g.err, fmt.Errorf("%v", r)) + g.mutex.Unlock() + } + }() if err := f(); err != nil { g.mutex.Lock() g.err = Append(g.err, err) g.mutex.Unlock() } }() }