funcmain(){ var n int = 1 var chs = make([]chanint, n) var worker = func(n int, c chanint){ for i:=0;i<n;i++{ c<-i } close(c) } //不定数量的channel数组 for i:=0;i<n;i++{ chs[i]=make(chanint) go worker(3+i, chs[i]) }
var selectCase = make([]reflect.SelectCase, n) //将channel绑定到SelectCase for i:=0;i<n;i++{ selectCase[i].Dir = reflect.SelectRecv //设置信道是接收,可以为下面值之一 /* const ( SelectSend SelectDir // case Chan <- Send SelectRecv // case <-Chan: SelectDefault // default ) */ selectCase[i].Chan = reflect.ValueOf(chs[i]) } numDone := 0 //从所有channel中取出最先到达的N个值 for numDone < n { chosen, recv, recvOk := reflect.Select(selectCase) if recvOk { fmt.Println(chosen, recv.Int(), recvOk) }else{ numDone++ } } }