V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
Grocker
V2EX  ›  Go 编程语言

go 通道输出结果问题

  •  
  •   Grocker · 2021-10-22 18:03:11 +08:00 · 2010 次点击
    这是一个创建于 907 天前的主题,其中的信息可能已经有所发展或是发生改变。
    package main
    
    import (
        "fmt"
    )
    
    func main() {
        c := make(chan int, 5)
        c <- 5
        c <- 6
        close(c)
        fmt.Println(<-c)
    }
    

    这段代码输出的结果为什么是 5 ?没太看明白

    16 条回复    2021-10-25 11:10:31 +08:00
    labulaka521
        1
    labulaka521  
       2021-10-22 18:11:00 +08:00
    先进先出
    5 进去了 6 进去了-> 5 出来了 6 出来了
    dugoj
        2
    dugoj  
       2021-10-22 18:13:16 +08:00
    类似先入先出的 FIFO 队列,你再 print 一下就是 6 了,或者 for range 遍历一下
    keepeye
        3
    keepeye  
       2021-10-22 18:15:33 +08:00
    队列啊 先进先出
    Grocker
        4
    Grocker  
    OP
       2021-10-22 18:16:51 +08:00
    @keepeye
    @dugoj
    @labulaka521 懂了谢谢,go 小白
    Keystroke
        5
    Keystroke  
       2021-10-22 18:18:11 +08:00 via Android
    我猜你是想问为什么 close 了还会接收到 5 而不是 0 。
    因为接收操作的底层逻辑是先 qcount > 0,即缓冲队列是否还有元素。再判断 closed != 0,即管道是否关闭。所以,5 和 6 取出后才是 0 。
    Keystroke
        6
    Keystroke  
       2021-10-22 18:19:36 +08:00 via Android
    好像是我想多了。
    labulaka521
        7
    labulaka521  
       2021-10-22 18:28:14 +08:00
    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.
    @Keystroke

    https://golang.org/ref/spec#Close
    Keystroke
        8
    Keystroke  
       2021-10-22 18:50:31 +08:00 via Android
    @labulaka521
    我说的不是一个意思吗?
    xfriday
        9
    xfriday  
       2021-10-22 21:01:22 +08:00
    @Keystroke chan 关闭了取出来的是零值,如果要判断 chan 是不是已经关闭了,可以这样:
    r, ok := <- c
    if ok {
    // ..
    }
    Keystroke
        10
    Keystroke  
       2021-10-22 21:24:21 +08:00 via Android
    @xfriday
    你先去试试楼主的代码最后一句改成你说的样子。看看 ok 是 true 还是 false 。
    xfriday
        11
    xfriday  
       2021-10-22 23:56:14 +08:00
    @Keystroke 当然是 true 啊,chan 里的数据有没用取完
    Keystroke
        12
    Keystroke  
       2021-10-23 00:05:46 +08:00 via Android
    @xfriday 那跟我最开始的回答不是一个意思吗?
    guanhui07
        13
    guanhui07  
       2021-10-23 11:27:33 +08:00
    先进先出
    xfriday
        14
    xfriday  
       2021-10-23 15:33:38 +08:00
    @Keystroke 我的意思就是别拿零值做判断而已,就像 r, err := foo(),别判断 r 是不是零值,而该判断 err 是不是 nil
    Keystroke
        15
    Keystroke  
       2021-10-23 16:41:48 +08:00   ❤️ 1
    @xfriday
    可能我没说清楚导致了一点误会。
    其实我是说 <- 操作符的底层源码逻辑是先判断 channel 的底层数据结构 hchan 的成员 qcount > 0 ,不是指判断取出的数据 > 0 ,包括后来说到 closed != 0 指的也是 hchan 中用于标识 channel 是否关闭的一个标志位。可能是这点导致了误会?
    你说的是正确的,<- 操作的第二返回值为 false 的语义是 channel 中没有有效数据且管道已关闭。只不过我是从源码层面解释了一下。
    Grocker
        16
    Grocker  
    OP
       2021-10-25 11:10:31 +08:00
    @Keystroke 没有想多,也有这方面的疑惑,@labulaka521 ,@Keystroke 谢谢解答
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3262 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:22 · PVG 21:22 · LAX 06:22 · JFK 09:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.