Re:Go Things I Love: Channels and Goroutines
by zamadatix on Tuesday January 07, 2020 @12:00PM
(#21981825)
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!"
[ Parent
| on HN ]
Re:Go Things I Love: Channels and Goroutines
by shhsshs on Tuesday January 07, 2020 @07:32PM
(#21986936)
I 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()