這是我的場景:我有5個分表,我想創建5個goroutine并并行讀取每個表,對于每個goroutine讀取資料,將撰寫另一個新表。如何使用 golang 以最佳方式設計它
uj5u.com熱心網友回復:
正如我提到的,您的設計已經完成。只需填寫作業代碼即可。
這是如何實作這種模式的粗略想法。
// This waitgroup will let us wait at the end until all goroutines are done.
var wg sync.Waitgroup
wg.Add(5)
// "I have 5 sharding tables"
for _, table := range [...]Table{table1, table2, table3, table4, table5} {
// "I wanna create 5 go routines"
go func(t Table) {
defer wg.Done()
// "and read each table in parallel"
readTable(t)
// "for each goroutine reading data, will write another new table"
writeNewTable()
}(table)
}
// Wait until all goroutines are done.
wg.Wait()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/430394.html
