zoukankan      html  css  js  c++  java
  • go channel select如何屏蔽已关闭通道

    go channel select如何屏蔽已关闭通道(如果不屏蔽的话,select 一直会处于选择状态)

    复制代码
    func main() {
        channel := make(chan int ,20)
        selectChannel(channel)
        getChannel(channel)
        time.Sleep(time.Hour)
    }
    
    func selectChannel(channel chan  int)  {
        go func() {
            for{
                select{
                    case _,ok := <- channel:
                        if !ok{
                            fmt.Println("channel is close")
                            channel = make(chan int) //设置为非缓冲通道已做阻塞
                            time.Sleep(time.Second)
                        }
                }
            }
        }()
    }
    
    func getChannel(channel chan  int)  {
        go func() {
            channel <- 2
            close(channel)
        }()
    }
    复制代码
  • 相关阅读:
    风险投资
    a blog about grid stytem
    960gs
    css布局学习资料
    rails测试
    telecommuting
    blueprint
    google +buttons style
    960gs
    960gs tips
  • 原文地址:https://www.cnblogs.com/ExMan/p/12622148.html
Copyright © 2011-2022 走看看