package main import ( "fmt" ) func main() { c := make(chan int, 10) c <- 1 c <- 2 c <- 3 close(c) for { fmt.Println(<-c) } }
判断短chan是否关闭
复制代码 代码如下:
package main import ( "fmt" ) func main() { c := make(chan int, 10) c <- 1 c <- 2 c <- 3 close(c) for { i, isClose := <-c if !isClose { fmt.Println("channel closed!") break } fmt.Println(i) } }