any order of which the select statements are evaluated?

151 views
Skip to first unread message

ated...@gmail.com

unread,
Oct 12, 2015, 8:45:38 AM10/12/15
to golang-nuts
Is there an order to the select statements are evaluated?

Given this example:

for {
  select {
  case inc := <-incoming:
    // process incoming data
  case out := <-outgoing:
    // send out outgoing data
  case <-done:
    // done
  }
}


Say incoming and outgoing channels are buffered and always crowded, is it possible that the 'done' channel will never get invoked because the loop always takes `incoming` first, then `outgoing` second, and then after that `done` if nothing is incoming and outgoing?

Jan Mercl

unread,
Oct 12, 2015, 8:49:17 AM10/12/15
to golang-nuts
On Mon, Oct 12, 2015 at 2:45 PM <ated...@gmail.com> wrote:

> Is there an order to the select statements are evaluated?

"""
For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, *in source order* ...
"""
Src: https://golang.org/ref/spec#Select_statements
--

-j

Roberto Zanotto

unread,
Oct 12, 2015, 9:05:08 AM10/12/15
to golang-nuts
Your first question is about evaluation order (which is technically what Jan answered), but your last question is if it's possible for some channels to starve. Short answer: no. If multiple channels can proceed, a pseudo-random choice is made. Chances of starvation are reeeeeeealy low. Read the whole paragraph about the select statement in the spec ;)
Reply all
Reply to author
Forward
0 new messages