go 基础select
go select
//多个channel协同分工,使用select来监听多个通道
//select和switch类似,case
func main() {
numchan1 := make(chan int)
numchan2 := make(chan int)
go func() {
for{
select {
case data1:= <- numchan1:
fmt.Println("读取data1数据",data1)
case data2 := <- numchan2:
fmt.Println("读取data2数据",data2)
}
}
}()
go func() {
for i:=0;i<30;i++ {
numchan1 <- i
fmt.Println("写入数据chan1",i)
time.Sleep(1*time.Second/2)
}
}()
go func() {
for i:=0;i<30;i++ {
numchan2 <-i
fmt.Println("写入数据chan2",i)
time.Sleep(1*time.Second)
}
}()
for{
time.Sleep(5*time.Second)
}
}