go 使用errgroup 捕获goroutine 运行中产生的错误

func CreateGreeter() error {
var mu sync.Mutex
group := new(errgroup.Group)
   var info []biz.Info
m := make(map[string]string)

userlist := []string{"User0001", "User0002", "User0003", "User0004", "User0005"}
for _, s := range userlist {
tempNum := s
go func() {}()
eg.Go(
func() error {
err := r.data.db.Table("111").Where("User=?", tempNum).Find(&info).Error
if err != nil {
fmt.Println("查询失败")
return err
}

news := m
for _, b := range info {
mu.Lock()
news[tempNum] = b.Function_ID
mu.Unlock()
}
return nil
},
)

}

if err := eg.Wait(); err != nil {
return err
}

return nil
}
posted @ 2022-01-01 12:36  雪糕战士  阅读(101)  评论(0编辑  收藏  举报