知识卡片
错误是显式返回的值
error是具有Error()string方法的接口。函数可同时返回业务结果和错误,调用者检查非nil错误并决定处理或传播;errors.New、格式化错误和自定义类型提供不同的信息组织方式。返回值承担业务结果,错误承担失败信息,不应让调用者仅靠日志猜测动作是否成功。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Go Language Foundations · 查看原书依据
Errors in Go are treated as just another value, represented by the built-in error type. This makes error handling straight-forward: idiomatic Go functions may include an error-typed value in its list of returns, which if not nil indicates an error state that may be handled via the primary execution path. For example, the os.Open function
参考来源
- 位置:《Cloud Native Go》第03章《Go Language Foundations》(来源项目book-sources;源文件:extracted/Cloud Native Go (Matthew A. Titmus)/_epub-src/OEBPS/ch03.xhtml)
- 结论依据:error是具有Error()string方法的接口。原文相应定义、示例或限制为此结论提供依据;补充纠错与版本边界在正文就近标明。
- 原始内容:Errors in Go are treated as just another value, represented by the built-in error type. This makes error handling straight-forward: idiomatic Go functions may include an error-typed value in its list of returns, which if not nil indicates an error state that may be handled via the primary execution path. For example, the os.Open function