dongxing4643 2016-04-26 05:10
浏览 22
已采纳

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

Reading the code from the bufio package I've found such things:

// 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")
    }
    ...
}

At the same time the Effective Go section about panic contains the next paragraph:

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.

So, I wonder, is the problem with a particular buffered reader so important to cause the panic call in the standard library code?

  • 写回答

1条回答 默认 最新

  • duanmei4362 2016-04-26 05:20
    关注

    It may be questionable, but consider: fill is a private method, and b.w and b.buf are private fields. If the condition that causes the panic is ever true, it's due to a logic error in the implementation of bufio. Since it should never really be possible to get into that state in the first place (a "can't happen" condition), we don't really know how we got there, and it's unclear how much other state got corrupted before the problem was detected and what, if anything, the user can do about it. In that kind of situation, a panic seems reasonable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端