Title: Proposal: Go 2 Error Inspection
Authors: Jonathan Amsterdam, Russ Cox, Marcel van Lohuizen, Damien Neil
Last updated: January 25, 2019
Discussion at golang.org/issue/29934
关键点:标题用一句话说清"做什么"("A built-in Go error check function, try"),并且永远附上讨论入口链接(issue 号),让文档不是孤立的。
摘要 / 概述(Abstract / Summary)
一段话讲完全文。读者读完这一段,应该已经知道你要做什么、大致怎么做。
泛型文档的摘要开门见山,一句话定义要做的事,紧接着埋下最重要的承诺:
We suggest extending the Go language to add optional type parameters to type and function declarations. ... The design is fully backward compatible with Go 1.
错误处理文档的摘要则点明改动范围和目标:
We propose several additions and changes to the standard library's errors and fmt packages ... codifying error wrapping ...
关键点:摘要里就要埋下最重要的承诺(如 "fully backward compatible with Go 1"),它往往是整个设计的隐含约束。
Briefly, the problem is that loops like this one don't do what they look like they do:
var ids []*int
for i := 0; i < 10; i++ {
ids = append(ids, &i)
}
That is, this code has a bug. After this loop executes, ids contains 10 identical pointers, each pointing at the value 10, instead of 10 distinct pointers to 0 through 9.
Loop variables being per-loop instead of per-iteration is the only design decision we know of in Go that makes programs incorrect more often than it makes them correct.
错误处理文档则点明痛点的普遍性——错误包装已经无处不在,普遍到值得收进标准库,同时坦白它的代价:
... one pattern has become so pervasive that we feel it is worth enshrining in the standard library ...
... indiscriminate wrapping can expose implementation details, introducing undesired coupling between packages ...
Don't confuse the term generic as used in this design with the same term in other languages like C++, C#, Java, or Rust ...
它把约束定位为"显式定义的结构化约束",而非声明式子类型:
Type constraints are interface types. ... explicitly defined structural constraints.
每个 API 都配 Go 声明 + 代码示例。slog 文档对 Logger/Record/Handler 三个核心类型,先给声明再给用法片段(如何用 slog.Group 分组、如何用 LogValuer 脱敏密码)。它对成功的定义也落在"一次安装、处处生效"上:
... the application can create a single handler and install it once for each logging library ... the benefits of a unified backend can be obtained with minimal code churn.
对照"改造前 vs 改造后"。try 文档把多行 if err != nil 和一行 f := try(os.Open(filename)) 并排展示,收益一目了然。
In most Go design documents, Rationale and Compatibility are two distinct sections. For this proposal, considerations of compatibility are so fundamental that it makes sense to address them as part of the rationale. To be completely clear: this is a breaking change to Go.
然后用三个机制把破坏性降到最低,并保证老代码原样运行:
... it only applies the new semantics to new programs, so that existing programs are guaranteed to continue to execute exactly as before.
通过 go.mod 里的 go 版本行按模块 opt-in。
用 //go:build 支持按文件逐步迁移(gradual code repair)。
把破例(打破"语言不重定义"的通则)说明为一次性的、深思熟虑的例外,理由是循环变量是 Go 里唯一一个"让程序错的次数多于对的次数"的设计。
错误处理文档则简洁声明:变更不违反 Go 1 兼容性承诺,同时诚实指出两处代价:
Gathering frame information may slow down errors.New slightly, but this is unlikely to affect practical programs.
Errors constructed with errors.New and fmt.Errorf will display differently with %+v.
关键点:诚实。承认代价(性能、行为变化),给出渐进迁移路径,用先例佐证。
实现 / 过渡计划(Implementation / Transition)
如何落地、分几步、配套什么工具。
错误处理文档:发布 golang.org/x/xerrors 兼容老版本,计划在 Go 1.13 周期初落地。
循环变量文档:提供静态编译标志和动态二分工具,并用 Google 内部全量启用的实测数据支撑"风险可控"——失败率约 1/8000,且几乎都是本就有 bug 的测试:
We have used this dynamic tooling in a conversion of Google's internal monorepo to the new loop semantics. The rate of test failure caused by the change was about 1 in 8,000.
### A decade of experience shows the cost of the current semantics
### Old code is unaffected, compiling exactly as before
### Changing the semantics is usually a no-op, and when it's not, it fixes buggy code far more often than it breaks correct code
读者光看标题就能读完整条论证链。每段内部也遵循"结论先行":段首一句给出观点,后面才是例子和数据。
关键点:一段只讲一件事,观点放段首。标题能写成一句话的论点,就别写成名词短语(写 "Old code is unaffected" 而不是 "Compatibility")。
语气:克制的诚实,甚至自嘲
Go 文档不端着。承认代价时直接说 "may slow down errors.New slightly";讲到这个 bug 多普遍时,作者连自己都不放过:
Russ certainly has done it repeatedly over the past decade, despite being the one who argued for the current semantics and then implemented them. (Apologies!)
这种"作者本人也踩过坑"的坦白,比任何"此问题影响重大"的形容词都更有说服力。
关键点:用事实和自嘲建立可信度,不用形容词堆砌权威感。强调要克制——_this is a breaking change_ 全文就斜体强调这一处,反而格外醒目。