3 ms·
This is indeed what Go is great for. But your code example is a bit wonky :) Specifically, for { select { case r := <-ch: // do
by redbad 14y ago
This is indeed what Go is great for. But your code example is a bit wonky :) Specifically,
for {
select {
case r := <-ch:
// do something with r
default:
fmt.Printf(".")
time.Sleep(5e7)
}
}
You're needlessly blocking 50ms on every iteration. (Side note: time.Sleep(50 * time.Millisecond) better captures your intent.) Better to reformulate that as
for {
select {
case r := <-ch:
// do something with r
case <-time.After(50*time.Millisecond):
fmt.Printf(".")
}
}
Or, if you don't need the dots in the output,
for i := 0; i < cap(ch); i++ {
r := <-ch
// do something with r
}
- mattetti 14y agoPost updated with some of your suggestions as well as some from @jgrahamc Thanks!