How does a select statement with a default case enable non-blocking channel operations?
Last updated
A plain
selectblocks until some send or receive case is ready. Adding adefaultcase makes it run that branch immediately when no channel is ready, so it never blocks.Typical uses: non-blocking receive, non-blocking send (drop if the buffer is full), and polling a done/quit channel inside a loop.
Caveat: looping tightly on
defaultbusy-spins the CPU, so add a timer or backoff.