Austin Clements (of the Go runtime team) wrote a paper that explores this in detail [1]. That was before joining the Go team, but the concepts are universal.
[1] https://people.csail.mit.edu/nickolai/papers/clements-sc.pdf
select {
case: <-chan1_whichIWantToCheckFirst
default:
}
select {
case: <-chan2_whichItreatTheSameAsChan3
case: 0xFF ->chan3_whichItreatTheSameAsChan2
} select {
case: <-chan2_whichItreatTheSameAsChan3 // a higher priority
case: 0xFF ->chan3_whichItreatTheSameAsChan2
}2) What in the world do you need such code for?
2) just read:
https://groups.google.com/g/golang-nuts/c/SXsgdpRK-mE/m/CT7UjJ3aBAAJ
https://groups.google.com/g/golang-nuts/c/ZrVIhHCrR9o
https://groups.google.com/g/golang-nuts/c/lEKehHH7kZY/m/SRmCtXDZAAAJI'm curious how?
> Not vice versa
There are pretty common patterns for this. At least for real word cases where you might have one special channel that you always want to check. Ugly, but in relation to the previous question, I don't see how one is doable and one isn't?
> I'm curious how?
if rand.Intn(2) == 0 {
select {
case: <-chan2_whichItreatTheSameAsChan3 // a higher priority
case: 0xFF ->chan3_whichItreatTheSameAsChan2
}
} else {
select {
case: 0xFF ->chan3_whichItreatTheSameAsChan2 // a higher priority
case: <-chan2_whichItreatTheSameAsChan3
}
}
Yes, it increases verbosity to the other way, but no performance loss.Doubling the number of instructions has no impact on run-time performance.
And there are more optimization opportunities in implementing a deterministic design. Now, the non-deterministic implementation needs to lock all involved channels before subsequent handling, a deterministic implementation might not need to.
In fact, in practice, sometimes, I do hope one specified case has a higher priority than others if they are all non-blocking.
It looks for most select blocks in Go code, it doesn't matter whether or not they are non-deterministic or deterministic.
But, if the default is deterministic, user code could simulate non-deterministic, without much performance loss. Not vice versa (the current design).