Go select

Go select is a control structure similar to switch but designed specifically for handling I/O operations and also for listening to data flow on channels. Each case must be a channel communication operation, either a send or a receive.

  1. If there is one case statement that can run, it is executed, and the rest are not executed
  2. If multiple case statements are ready to run, select randomly chooses one to execute, and the rest are not executed
  3. If there are no runnable case statements but there is a default statement, the default will be executed (note that the default statement in select is always runnable, which can be CPU-intensive, hence it's generally advised not to include it unless necessary)
  4. If there are no runnable case statements and no default statement, select will block until at least one case statement becomes runnable
select {
   case communication1:
      //...      
   case communication2:
      //...
   case communication3:
      //...
   default :
      //...
}

 

Take a break

πŸ‘‰πŸ‘‰πŸ‘‰ 【Jade Dynasty EP49】Gui Li and Xiao Bai went to the Qilidong and competed with the locals in drinking