为什么 Go 的 bufio 在后台使用 panic?

Why Go's bufio uses panic under the hood?

bufio 包中读取代码,我 found 这样的事情:

// fill reads a new chunk into the buffer.
func (b *Reader) fill() {
    ...
    if b.w >= len(b.buf) {
        panic("bufio: tried to fill full buffer")
    }
    ...
}

同时Effective Gosection关于panic包含下一段:

This is only an example but real library functions should avoid panic. If the problem can be masked or worked around, it's always better to let things continue to run rather than taking down the whole program.

所以,我想知道,特定缓冲 reader 的问题是否如此重要以导致标准库代码中的 panic 调用?

可能会有疑问,但考虑一下:fill是私有方法,b.wb.buf是私有字段。如果导致恐慌的条件永远为真,那是由于 bufio 的实现中的逻辑错误。由于一开始就不可能真正进入该状态("can't happen" 条件),我们真的不知道如何 我们到达那里,目前还不清楚在检测到问题之前有多少其他状态被破坏,以及用户可以做什么(如果有的话)。在那种情况下,恐慌似乎是合理的。