下面是场景:
inputCh := g(abc) // return <- chan []string
slice := make([]map[string][]string, 0)
m := sync.Mutex{}
for whatever := range inputCh {
go func(whatever []string) {
matches := f(xyz, whatever) // returns map[string][]string
m.Lock()
slice = append(slice, matches)
m.Unlock()
}(whatever)
}
z := merge(slice...)
我们不知道的地方,何时inputCh关闭(close(inputCh))
merge(slice...)我们只需要在所有 go-routines 完成和更新后调用slice
不确定,如果sync.WaitGroup可以使用。
如何确保只有在所有 go-routines 更新merge(slice...)后才被调用?slice
慕丝7291255
呼如林
相关分类