Tony Bai

终结十年纠结:Go 新提案允许 Example 支持任意函数签名 (opens in new tab)

本文永久链接 – 大家好,我是Tony Bai。 在 Go 语言的开发日常中,编写 ExampleXxx 示例代码不仅是完善文档的必经之路,更是一门绝佳的“活文档”艺术。 通过在 “_test.go” 文件中编写以 Example 开头的函数,并在末尾加上 // Output: 注释,Go 官方的 go doc 和 pkgsite 就能在网页上直接渲染出可交互、可直接在浏览器中运行(Playable)的示例代码。 然而,在这个看似优雅的设计背后,却隐藏着一个让全球 Go 工程师如鲠在喉、折磨了大家近十年的“硬伤”:Go 官方对 Example 函数的签名有着极其死板的限制——它必须是无参、无返回值的空函数。 为了这个限制,我们在写文档示例时,不得不写出大量极度违背 Go 地道(Idiomatic)编程哲学的样板代码。 为了彻底拔掉这根刺,Go 编译器团队核心成员Damien Neil (neild)提交了一项被称为“大一统”的提案——Issue #79808:允许 Example 示例支持任意函数签名! 这项提案不仅终结了社区自 2017 年以来的长期争论(#21111 与 #6...

Read the original article
Sign in to keep reading the full article.

Keyboard Shortcuts

Navigation

Next / previous post
j/k
Open post
oorEnter
Preview post
v

Post Actions

Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Save / unsave
s

Recommendations

Add interest / feed
Enter
Not interested
x

Go to

Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Discover
gb
Search
/

General

Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help