channelselectcase<-
select {case <- chan1: // do somethingcase <- chan2: // do something}
chan1chan2channeldefalut
selectcasedefaultselectcasedefaultcasecasecasedefalutselect
channelchannelselectchannelselectcasedefaultchanneldefalutselectselectcasedefaultselectcase
channelselectselectchannel
也就是说,当高优先级和低优先级都有数据时,高优先级先被处理,也就是实现了优先级。示例如下:
for { select { case data := <- highChan:
handleHigh(data) default: select { case data := <- highChan:
handleHigh(data) case data := <- lowChan:
handleLow(data)
}
}
}