Go Select Implementation
- The select mechanism in Go is an important and unique feature. Current select mechanism implementation by the official https://github.com/golang/go/blob/master/src/runtime/select.go.
There are several steps to execute a select block:
- Evaluate all involved channels and values to be potentially sent, from top to bottom and left to right.
- Randomise the case orders for polling (the default branch is treated as a special case). The corresponding channels of the orders may be duplicate. The default branch is always put at the last position.
- sort all involved channels to avoid deadlock in the next step. No duplicate channels are in the first N channels of the sorted result, where N is the number of involved channels in the select block. Below, the sorted lock orders mean the the first N ones.
- lock all involved channels by the sorted lock orders in last step.
- poll each cases in the select block by the randomised case orders: