ARTICLE AD BOX
If you want to read all values from a channel ch until it is closed, you can do so with range:
for x := range ch { // do something with x }This will block until ch is closed. I want to read all values currently available, from a channel that does not get closed after the last value was sent. I have come up with two solutions:
// option 1 loop: for { select { case x := <- ch: // do something with x default: break loop } } // option 2 func() { for { select { case x := <- ch: // do something with x default: return } } }()Is there a simpler or shorter way of doing this, like with range?
