That's a good read. The comments note that the compiler
doesn't call the general case code for select statements
with only 1 case plus a default. But if you use the
"Select()" function, you grind through the general case,
which is quite elaborate.
I'd wondered how they did that. The answer is that it works
in the obvious, but slow way. It creates a selection object,
fills it in with all the possible selections, randomizes the order
of the selections, tests them in sequence, picks the first winner,
and goes with that.
If there's no winner and the select has to block, it hangs
an event that references the select on each involved channel
and parks to await further developments. When something happens
on a relevant channel, this code wakes up and another pass is
made through the selection algorithm.
The randomizer is at
http://tip.golang.org/src/pkg/runtime/chan.c?h=rselect#L866
It generates a random number for each case in the select, then
does a bubble sort on the results, then tests all the cases in
the permuted sequence. It's thus O(n^2). Selects on hundreds
or thousands of channels are probably not a good idea unless
somebody speeds up that code.
The C code looks retro. There are many "goto" statements,
going up, down, and sideways. All variable declarations are at the
beginning of each function. Very few comments. It's like reading
the source code for early UNIX tools from the K&R era. Nostalgic.
John Nagle