We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
你好,我在看书的时候,对其中 channel 容量的有些疑问,所以想问一下我的理解哪里有问题,谢谢
golang101/articles/channel-use-cases.html
Lines 1010 to 1047 in d72f4ca
func source() <-chan int32 { c := make(chan int32, 1) // 必须为一个缓冲通道 go func() { ra, rb := rand.Int31(), rand.Intn(3)+1 time.Sleep(time.Duration(rb) * time.Second) c <- ra }() return c }
不知道为什么必须为 1,即使为 0 也完全正常
Lines 1084 to 1115 in d72f4ca
c := make(chan struct{}, 1) // 容量必须严格为1
此处的容量为什么严格为1,容量为 0 没有问题呀?
上面两处的容量为 0 都可以正常运行,而且不存在 “数据请求者因为种种原因未及时准备好接收,则所有回应者的尝试发送都将失败,从而所有回应的数据都将被错过。”
The text was updated successfully, but these errors were encountered:
对于第一个,如果不为缓冲通道的话,则两个未被选择的case对应的协程将处于永久阻塞状态,造成内存泄露。
对于第二个,缓冲为0的情况其实也可以,具体取决于具体应用的要求。缓冲为0时,(站在接收者的角度),每两个连续接收操作的间隔肯定大于d;而缓冲为1时,(站在发送者的角度),尽量保证在每个Nd时刻发送一个信号。其实缓冲大于1也不是一点道理没有,但是容易造成瞬时发生很多接受操作的情况。
这里缓冲取1是为了和标准库包中的time.Tick函数的行为保持一致。
不过一些描述确实需要再调整一下。多谢提出这个问题。
Sorry, something went wrong.
@TapirLiu 非常感谢解答,而且我注意到线上图书已经更新了,辛苦了。
No branches or pull requests
你好,我在看书的时候,对其中 channel 容量的有些疑问,所以想问一下我的理解哪里有问题,谢谢
golang101/articles/channel-use-cases.html
Lines 1010 to 1047 in d72f4ca
不知道为什么必须为 1,即使为 0 也完全正常
golang101/articles/channel-use-cases.html
Lines 1084 to 1115 in d72f4ca
此处的容量为什么严格为1,容量为 0 没有问题呀?
上面两处的容量为 0 都可以正常运行,而且不存在 “数据请求者因为种种原因未及时准备好接收,则所有回应者的尝试发送都将失败,从而所有回应的数据都将被错过。”
The text was updated successfully, but these errors were encountered: