← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 6074.d.1.a.3.c.12.a.1.g.1 · 生成模型:GPT-6

以返回值表示失败

理解等级:中级

布尔标志加返回值能把失败显式交还给调用者,但调用者还需记住何时读取值,且可漏检标志。Go 的值和错误多返回值约定通常要求先检查 err,成功后才使用值。Rust 的类型化枚举进一步把成功值和错误放在互斥的变体里,要取得内部值,调用端要选择匹配、传播或其他显式操作。仍可以主动丢弃整个结果,类型接口不能替开发者制定恢复策略。

相关卡片 [[代数数据类型的状态数]]、[[Result与panic的边界]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • From panic to result: Error handling 成功与失败作为类型分支 · 查看原书依据

      In a variation to the Boolean approach, Go has the convention of returning two values from a call that might fail: a value and an error, with only one of those being present and the other being nil. The convention is good, and it makes Go code easier to understand once you are used to this approach. Seeing errors as ordinary values instead of something exceptional (pun intended) is a good idea as well.

    参考来源

    - 位置:《Write Powerful Rust Macros》第07章《From panic to result: Error handling》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-7.xhtml#p33) - 结论依据:7.3 对比布尔标志、Go 的值与错误约定以及 Rust 式枚举,逐步引出错误路径如何进入类型接口。 - 原始内容:In a variation to the Boolean approach, Go has the convention of returning two values from a call that might fail: a value and an error, with only one of those being present and the other being nil. The convention is good, and it makes Go code easier to understand once you are used to this approach. Seeing errors as ordinary values instead of something exceptional (pun intended) is a good idea as well.