标签:case ack for fun 复用 sel out sleep 管道
package main import ( "fmt" "time" ) /* ·循环从一写两读三条管道中随机选择一条能走的路 ·等所有路都走不通了就退出循环 */ func main041() { chA := make(chan int, 5) chB := make(chan int, 4) chB <- 123 chB <- 123 chB <- 123 chB <- 123 chC := make(chan int, 3) chC <- 123 chC <- 123 chC <- 123 go TaskA(chA) go TaskB(chB) go TaskC(chC) time.Sleep(10 * time.Second) fmt.Println("main over") } func main() { chA := make(chan int, 5) chB := make(chan int, 4) chB <- 123 chB <- 123 chB <- 123 chB <- 123 chC := make(chan int, 3) chC <- 123 chC <- 123 chC <- 123 OUTER: for true { /*随机选择一条能走通的case 所有case都走不通时,走default 可以通过break跳出select,break XXX跳出指定标签*/ select { case chA <- 123: fmt.Println("执行任务A") time.Sleep(time.Second) case x := <-chB: fmt.Println("执行任务B", x) time.Sleep(time.Second) case <-chC: fmt.Println("执行任务C") time.Sleep(time.Second) default: fmt.Println("全部任务已结束") //break//跳出select break OUTER } } fmt.Println("main over") } func TaskA(ch chan int) { for true { fmt.Println("TaskA") ch <- 123 time.Sleep(time.Second) } } func TaskB(ch chan int) { for true { fmt.Println("TaskB") <-ch time.Sleep(time.Second) } } func TaskC(ch chan int) { for true { fmt.Println("TaskC") <-ch time.Sleep(time.Second) } }
标签:case ack for fun 复用 sel out sleep 管道
原文地址:https://www.cnblogs.com/yunweiqiang/p/12769689.html