3 ms·
I wish WaitGroups weren't so awkward syntax wise... "Here is an easy way to keep track of a bunch of Async tasks you launch as a group! Just don't forget to inc
by zamadatix 7y ago
I wish WaitGroups weren't so awkward syntax wise... "Here is an easy way to keep track of a bunch of Async tasks you launch as a group! Just don't forget to increment this counter each time you launch one or it won't work!"
- shhsshs 7y agoI prefer to call add once if I know how many routines I will create. var wg sync.WaitGroup n := 10 wg.Add(n) for i := 0; i < n; i++ { go func() { defer wg.Done() doSomething() }() } wg.Wait() You still have to call wg.Done(), unfortunately. People have written small wrappers around sync.WaitGroup that change the interface: var wg bettersync.WaitGroup n := 10 for i := 0; i < n; i++ { wg.Go(func() { doSomething() }) } wg.Wait()