12年后,Go终于默认支持泛型

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根据 Go 语言社区发布的 2020 年度调查报告表明,88% 的受访者认为泛型是 Go 缺失的关键特性。"}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Go 默认支持泛型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"近日,"},{"type":"link","attrs":{"href":"https:\/\/go-review.googlesource.com\/c\/go\/+\/343732\/","title":"xxx","type":null},"content":[{"type":"text","text":"Go 项目代码仓库"}]},{"type":"text","text":"提交和合并的一个 PR 显示,Go 语言已在 cmd\/compile 中默认启用 -G=3,可使用新的 types2 类型检查器并支持类型参数。这意味着,Go 编译器正式启用了对泛型的支持。根据描述,在此之前,cmd\/compile 的 -G flag 默认值为 0。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/de\/f8\/ded328be59713711059dc1a741674cf8.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"事实上,在上周 Go 1.17 发布时,就有开发者发现泛型代码已被合并,只是默认不启用。而随着 -G flag 默认值由 0 改成 3,加速奔跑的 Go 终于倾听广大开发者的声音,支持泛型。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Go 语言起源于 2007 年,并于 2009 年正式发布。在这十余年中,向 Go 语言添加泛型的讨论一直持续着。有开发者悲观地认为,Go 语言可能永远都不会加入泛型了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根据 Go 语言社区发布的 2019 年度调查报告表明,79% 的受访者认为泛型是 Go 缺失的关键特性。而在"},{"type":"link","attrs":{"href":"https:\/\/www.infoq.cn\/article\/tjhDZBta1K5arfDjXOA5","title":"xxx","type":null},"content":[{"type":"text","text":" 2020 年的开发者调查报告"}]},{"type":"text","text":"中,这一比例达到了 88%。此外,还有 18% 的受访者表示,由于缺少泛型而不会用 Go。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2019 年 7 月底,Go 团队发布了 Go 2 泛型设计的草稿 Contracts - Draft Design,这个设计草稿建议增加参数多态来扩展 Go 语言。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2020 年 6 月下旬,Go 团队发布了关于泛型的"},{"type":"link","attrs":{"href":"https:\/\/mp.weixin.qq.com\/s?__biz=MzIzNjUxMzk2NQ==&mid=2247502431&idx=1&sn=9e64202ae8be1c255c675046e57175e3&chksm=e8d4359ddfa3bc8b85cb4d9274898a10ffcfd93d17178528c28c8281bf79d6035b507fcd762d&scene=27#wechat_redirect","title":"xxx","type":null},"content":[{"type":"text","text":"最新设计草案"}]},{"type":"text","text":",此后一直在完善相关工作,并将注意力转移到生产就绪版本的实现身上。Go 团队称,“我们将在 2021 年年内继续努力,力争在年底前为大家带来一些可供试用的成果,也许会以 Go 1.18 beta 的形式发布。”"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2021 年 1 月,Go 团队核心成员 Ian Lance Taylor 宣布已提交为 Go 添加泛型的提案,并表示“为 Go 添加泛型的语言变更完全向后兼容,现有的 Go 程序会继续像现在一样正常运行。”"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这是 Go 泛型特性的又一步历史性前进。根据 Go 官方消息,Go 1.18 中将正式启用泛型。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"为什么 Go 语言没有泛型?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从诞生到现在,12 年的 "},{"type":"link","attrs":{"href":"https:\/\/www.infoq.cn\/article\/9SS2aGyI3951oBfb3OTP","title":"xxx","type":null},"content":[{"type":"text","text":"Go 为什么一直没有泛型"}]},{"type":"text","text":"?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"简单来说有以下两点原因:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"泛型困境是所有编程语言都需要面对的,也是加入泛型之前不得不深思熟虑的问题;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"目前的多数泛型提案都有明显的缺陷,而且在 1.x 版本中,提升语言其他方面性能带来的收益比泛型带来的更多。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一方面,泛型和其他特性一样,不是只有好处,也有坏处,为编程语言加入泛型会遇到需要权衡的两难问题。语言的设计者需要在编程效率、编译速度和运行速度三者进行权衡和选择,编程语言要选择牺牲一个而保留另外两个。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/b9\/26\/b9f565de8b2db126c3073696c7fff626.jpg","alt":null,"title":"泛型困境,图片来源:Draveness 技术网站","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当我们考虑是否应该支持泛型时,实际上需要考虑的问题是:我们应该牺牲工程师的开发效率、牺牲编译速度和更大的编译产物还是牺牲运行速度。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"泛型的引入一定会影响编译速度和运行速度,同时也会增加编译器的复杂度,所以社区在考虑泛型时也非常谨慎。Go 2 的泛型提案在面对这个问题时没有进行选择,让具体实现决定是应该影响编译速度(单独编译不同的类型参数)还是运行时间(使用方法调用在运行时决定具体执行的函数)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"另一方面,社区中的大部分泛型提案都有各自的缺陷,所以不会被 Go 团队采纳,同时向 Go 语言中加入泛型并不是团队的首要工作,所以 Go 语言发布 10 多年以来一直都没有支持泛型。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当前,虽然 Go 编译器已默认启用 -G=3,但 -G=0 模式仍在测试中。对于渴望支持泛型的开发者来说,一起期待明年的 Go 1.18 吧。"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章