ch := make(chan bool)
close(ch)
close(ch)  // 这样会panic的,channel不能close两次

读取的时候channel提前关闭了

ch := make(chan string)
close(ch)
i := <- ch // 不会panic, i读取到的值是空 "",  如果channel是bool的,那么读取到的是false

向已经关闭的channel写数据

ch := make(chan string)
close(ch)
ch <- "good" // 会panic的

判断channel是否close

i, ok := <- ch
if ok {
    println(i)
} else {
    println("channel closed")
}

for循环读取channel

for i := range ch { // ch关闭时,for循环会自动结束
    println(i)
}

防止读取超时

select {
    case <- time.After(time.Second*2):
        println("read channel timeout")
    case i := <- ch:
        println(i)
}

防止写入超时

// 其实和读取超时很像
select {
    case <- time.After(time.Second *2):
        println("write channel timeout")
    case ch <- "hello":
        println("write ok")
}

 

出处: http://my.oschina.net/goskyblue/blog/191149

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-30
  • 2022-12-23
  • 2021-09-14
  • 2021-10-22
  • 2022-12-23
  • 2021-12-28
猜你喜欢
  • 2021-08-05
  • 2022-12-23
  • 2021-10-13
  • 2023-02-01
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案