chan

func main(){
a:=1
b:=2
fmt.Println(a+b)
c:=make(chan struct{})
go func (){
for {
_,ok:=<-c
if ok{
fmt.Println("ok")
}else{
fmt.Println("not ok")
}
}
}()

tick := time.NewTicker(1 * time.Second)
select {
case <-tick.C :
close(c)
//case c<- struct{}{} :
fmt.Println("select")
}
//c<-struct{}{}
}




not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok
not ok


不论是否有缓冲的chan,关闭后都可以读数据,不会阻塞


Category golang