示例代码:
package main
import (
"fmt"
"time"
)
//定义 读写通道 函数
func chanFunc1(c chan string) {
c <- "YDOOK.COM"
}
func chanFunc2(c chan string) {
c <- "www.ydook.com"
}
func main() {
c1 := make(chan string)
c2 := make(chan string)
go chanFunc1(c1)
go chanFunc2(c2)
// 根据通道返回数据的时间先后选择判断
select {
case ms := <-c1:
fmt.Println("chanFunc1 actived recevied: ms = ", ms)
case ms := <-c2:
fmt.Println("chanFunc2 actived recevied: ms = ", ms)
// 设置通道不响应:延时退出
case <-time.After(time.Second * 5):
fmt.Println("channel is no responsed !")
}
}
运行代码:
chanFunc1 actived recevied: ms = YDOOK.COM