doushi8231 2014-10-10 09:36
浏览 41
已采纳

有关进入频道的一些问题

1-what is the condition that make chan break?

deliveries <-chan amqp.Delivery
for d:= range deliveries{
    ..
}

If there is no more data in chan deliveries about a few minutes,that it will break. Is the code up is same to below?

deliveries <- chan amqp.Delivery
for{
    d,ok:=<-deliveries
    if !ok{
        break
    }
    //code
}

2-Why does chan not only return data but also status?And what does the "ok" mean?

3-How does the chan realize?"ok" is the status about client,Why can it return the "ok"?

  • 写回答

2条回答 默认 最新

  • dongqiaolong9034 2014-10-10 13:14
    关注

    I will answer question 2 and 3 first because the answer provides context for my answer to question 1.

    2, 3) The builtin function close(c) records that no more values will be sent to the channel c.

    The second result in a receive expression is a bool indicating if the operation was successful. The second result is true if a sent value was received or false if the zero value was received because the channel was closed.

    1) Range over a channel receives values sent on the channel until the channel is closed.

    The following loops are very similar. They both receive values until the channel is closed.

    for v := range c {
         // code
    }
    
    for {
        v, ok := <-c
        if != ok {
            break
        }
        // code
    }
    

    The main difference between these loops is the scope of of the variable v. The scope of v is outside of the first loop and inside the second loop. This distinction is important if you use a closure and goroutine in the loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大