You absolutely can use goroutines without waitgroups! It all depends on 
what you are doing with them. Where waitgroups come in is when you need to 
something only after *all* of the goroutines on a specific task are done.

Chances are that if you are not using waitgroups, you are either using 
channels or you are only interested in the side-effects of the goroutines 
and are running a long-running service that does not need to have a clear 
end-point.

For example, there is a spot in one of my code-bases where I fire off a few 
optimization routines in parallel - each one takes a channel to feed a 
result in, and I know going in exactly how many results there will be. I am 
going to be comparing and keepign the best, so I don't care which routine 
returns its result first, last, etc - I simply pull the known number of 
results out of the channel - knowing that it will block until that number 
of goroutines has returned. This is a known number of routines though, and 
a waitgroup would be a viable alternative here.

I also have two other kinds of programs that don't use waitgroups - one 
sort of them builds a chain of channels with a goroutine pushing data 
through, and all the other goroutines just flow data through the channel 
using range until the channel closes, then they evaporate. Doesn't need a 
waitgroup because they aren't working on 'pieces of the data that have to 
be put back together at the end' but instead are each operating on *all* 
the data as it flows through them. And the others fire off a goroutine in 
response to an incoming connection, handle that connection, and then go 
away. No need for waitgroups there, unless they in turn fire off a 
collective operation.

A waitgroup is just a way of saying, "I need to be able to wait until all 
these goroutines have done there stuff and finished." If you don't need to 
wait for a group of them to finish, you don't need a waitgroup.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to