package main import (
"fmt"
"time"
) func main() {
ch := make(chan int)
quit := make(chan bool) go func() {
for true {
select {
case num := <- ch:
fmt.Println("num = ", num)
case <-time.After(*time.Second):
quit<-true
}
}
}()
for i:=; i<; i++ {
ch<-i
time.Sleep(time.Second)
}
<-quit
fmt.Println("程序结束")
}
打印的结果
num =
num =
num =
num =
num =
num =
num =
num =
num =
程序结束