要开始执行两个goroutine的无限循环,我可以使用下面的代码:
收到消息后,它将启动一个新的goroutine并继续下去.
c1 := make(chan string)
c2 := make(chan string)
go DoStuff(c1, 5)
go DoStuff(c2, 2)
for ; true; {
select {
case msg1 := <-c1:
fmt.Println("received ", msg1)
go DoStuff(c1, 1)
case msg2 := <-c2:
fmt.Println("received ", msg2)
go DoStuff(c2, 9)
}
}
Run Code Online (Sandbox Code Playgroud)
我现在想对N goroutines有相同的行为,但是在这种情况下select语句会如何?
这是我开始使用的代码位,但我很困惑如何编写select语句
numChans := 2
//I keep the channels in this slice, and want to "loop" over them in the select statemnt
var chans = [] chan string{}
for i:=0;i<numChans;i++{
tmp := make(chan string);
chans …Run Code Online (Sandbox Code Playgroud) go ×1