想实现一个主程序在监听多个channel的练习。如下,我理解只输出一次就会退出,但是实际上,似乎跑了个死循环,问题出在哪里?
```golang
package main
import (
"fmt"
"time"
)
func put(c chan<- string, msg string) {
time.Sleep(time.Second * 1)
//for i := 1; i < 10; i++ {
c <- msg
//}
close(c)
}
func main() {
c1 := make(chan string)
c2 := make(chan string)
go put(c1, "c1 echoing")
go put(c2, "c2 echoing")
for {
select {
case <-c1:
fmt.Println("c1 echoing")
case <-c2:
fmt.Println("c2 echoing")
}
}
}
```
有疑问加站长微信联系(非本文作者)