标签:sele 复用 make 需要 结构 print 关闭 ISE 使用
通道(Channel)
/* 通道(channel)是用来传递数据的一个数据结构。 */ ch1 := make(chan int ,2) //创建一个可读可写的双向管道 ch1 <- 10 ch1 <- 12 m1 := <-ch1 m2 := <-ch1 fmt.Println(m1,m2) ch2 := make(chan <- int ,2) //创建一个只可写的管道 ch2 <- 10 ch2 <- 12 ch3 := make(<-chan int ,2) //创建一个只可读的管道
select(多路复用)
//定义个管道 10个数据int
intChan := make(chan int,10)
for i:=1 ; i< 10 ;i++ {
intChan <- i
}
//定义个管道 10个 数据string
stringChan := make(chan string ,10)
for i:=1 ;i < 5 ;i++ {
stringChan <- "hello"+fmt.Sprintf("%d",i)
}
//使用select不需要关闭channel
for {
select {
case v := <-intChan:
fmt.Println("从intChan里面取数据%d",v)
time.Sleep(time.Millisecond * 50)
case v := <-stringChan:
fmt.Println("从stringChan里面取数据%v",v)
time.Sleep(time.Millisecond * 50)
default:
fmt.Println("所有数据获取完毕")
return //跳出循环
}
}
标签:sele 复用 make 需要 结构 print 关闭 ISE 使用
原文地址:https://www.cnblogs.com/finnlee/p/14165538.html