What is the Advantage of sync.WaitGroup over Channels?

Independently of the correctness of your second example (as explained in the comments, you aren’t doing what you think, but it’s easily fixable), I tend to think that the first example is easier to grasp.

Now, I wouldn’t even say that channels are more idiomatic. Channels being a signature feature of the Go language shouldn’t mean that it is idiomatic to use them whenever possible. What is idiomatic in Go is to use the simplest and easiest to understand solution: here, the WaitGroup convey both the meaning (your main function is Waiting for workers to be done) and the mechanic (the workers notify when they are Done).

Unless you’re in a very specific case, I don’t recommend using the channel solution here.

Leave a Comment