Home > Software engineering >  how to design scenario by goroutine
how to design scenario by goroutine

Time:02-22

here's my scenario:I have 5 sharding tables, I wanna create 5 go routines and read each table in parallel, for each goroutine reading data, will write another new table. How to design it in best way by using golang

CodePudding user response:

As I mention, your design is complete already. Just a matter of filling in the working code.

Here is a rough idea of how to implement this kind of pattern.

// 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 in [...]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()
  • Related