go 通道输出结果问题

2021-10-22 18:03:11 +08:00
 Grocker
package main

import (
    "fmt"
)

func main() {
    c := make(chan int, 5)
    c <- 5
    c <- 6
    close(c)
    fmt.Println(<-c)
}

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

2019 次点击
所在节点    Go 编程语言
16 条回复
labulaka521
2021-10-22 18:11:00 +08:00
先进先出
5 进去了 6 进去了-> 5 出来了 6 出来了
dugoj
2021-10-22 18:13:16 +08:00
类似先入先出的 FIFO 队列,你再 print 一下就是 6 了,或者 for range 遍历一下
keepeye
2021-10-22 18:15:33 +08:00
队列啊 先进先出
Grocker
2021-10-22 18:16:51 +08:00
@keepeye
@dugoj
@labulaka521 懂了谢谢,go 小白
Keystroke
2021-10-22 18:18:11 +08:00
我猜你是想问为什么 close 了还会接收到 5 而不是 0 。
因为接收操作的底层逻辑是先 qcount > 0,即缓冲队列是否还有元素。再判断 closed != 0,即管道是否关闭。所以,5 和 6 取出后才是 0 。
Keystroke
2021-10-22 18:19:36 +08:00
好像是我想多了。
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
2021-10-22 18:50:31 +08:00
@labulaka521
我说的不是一个意思吗?
xfriday
2021-10-22 21:01:22 +08:00
@Keystroke chan 关闭了取出来的是零值,如果要判断 chan 是不是已经关闭了,可以这样:
r, ok := <- c
if ok {
// ..
}
Keystroke
2021-10-22 21:24:21 +08:00
@xfriday
你先去试试楼主的代码最后一句改成你说的样子。看看 ok 是 true 还是 false 。
xfriday
2021-10-22 23:56:14 +08:00
@Keystroke 当然是 true 啊,chan 里的数据有没用取完
Keystroke
2021-10-23 00:05:46 +08:00
@xfriday 那跟我最开始的回答不是一个意思吗?
guanhui07
2021-10-23 11:27:33 +08:00
先进先出
xfriday
2021-10-23 15:33:38 +08:00
@Keystroke 我的意思就是别拿零值做判断而已,就像 r, err := foo(),别判断 r 是不是零值,而该判断 err 是不是 nil
Keystroke
2021-10-23 16:41:48 +08:00
@xfriday
可能我没说清楚导致了一点误会。
其实我是说 <- 操作符的底层源码逻辑是先判断 channel 的底层数据结构 hchan 的成员 qcount > 0 ,不是指判断取出的数据 > 0 ,包括后来说到 closed != 0 指的也是 hchan 中用于标识 channel 是否关闭的一个标志位。可能是这点导致了误会?
你说的是正确的,<- 操作的第二返回值为 false 的语义是 channel 中没有有效数据且管道已关闭。只不过我是从源码层面解释了一下。
Grocker
2021-10-25 11:10:31 +08:00
@Keystroke 没有想多,也有这方面的疑惑,@labulaka521 ,@Keystroke 谢谢解答

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/809873

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX