3 ms·
Spin several goroutines, do a `select` on them. It's right in the Tour of Go. Cancellation tokens are harder, though.
by nine_k 8y ago
Spin several goroutines, do a `select` on them. It's right in the Tour of Go.
Cancellation tokens are harder, though.
- tonyarkles 8y agoIt’s been a while since I’ve been writing Go, but wasn’t that nominally what the Context objects were supposed to be for? I think they were introduced a bit late in the game, but APIs that supported them handled cancellation well (if I’m remembering right)
- owaislone 8y agoContext would be passed to each go-routines for cancellation but the pattern would be the same as OP's suggestion. I'd say a set of go-routines and a sync.WaitGroup would probably be a better pattern for this use case. Routines + select is more useful for listening on incoming data than concurrently sending out bytes.