News for hackers. Stuff that matters.
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()

There are no comments on this comment yet.