douyi4205 2015-03-26 02:01
浏览 11
已采纳

为什么我从封闭的渠道接收价值?

I'm investigating the channels behaviour and I'm quite confused by their behaviour. The spec say After calling close, and after any previously sent values have been received, receive operations will return the zero value for the channel's type without blocking. However it seems I still get the values in the range statement even if by that time the channel is closed. Why is that?

package main

import "fmt"
import "sync"
import "time"

func main() {
    iCh := make(chan int, 99)
    var wg sync.WaitGroup
    go func() {
        for i := 0; i < 5; i++ {
            wg.Add(1)
            go func(i int) {
                defer wg.Done()
                iCh <- i
            }(i)

        }
        wg.Wait()
        close(iCh)
    }()
    time.Sleep(5 * time.Second)
    print("the channel should be closed by now
")
    for i := range iCh {
        fmt.Printf("%v
", i)
    }
    print("done")
}

Edit: It seems that if I move the close statement just before the channel range it closes it for good. So I'm wondering why it's not working with the "time.Sleep" trick too . By that time (5 seconds) all the go routines should have been completed and the channel closed, isn't it ?

  • 写回答

1条回答 默认 最新

  • dotj6816 2015-03-26 02:13
    关注

    The Go Programming Language Specification

    Close

    For a channel c, the built-in function close(c) records that no more values will be sent on the channel. After calling close, and after any previously sent values have been received, receive operations will return the zero value for the channel's type without blocking.

    In the channel buffer there are 5 previously sent values followed by a close.

    For example,

    package main
    
    import (
        "fmt"
        "sync"
        "time"
    )
    
    func main() {
        iCh := make(chan int, 99)
        var wg sync.WaitGroup
        go func() {
            for i := 0; i < 5; i++ {
                wg.Add(1)
                go func(i int) {
                    defer wg.Done()
                    iCh <- i
                }(i)
    
            }
            wg.Wait()
            close(iCh)
        }()
    
        time.Sleep(5 * time.Second)
        fmt.Println("previously sent values", len(iCh))
        for i := range iCh {
            fmt.Printf("%v
    ", i)
        }
        print("the channel should be closed by now
    ")
        print("done")
    }
    

    Output:

    previously sent values 5
    0
    1
    2
    3
    4
    the channel should be closed by now
    done
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题