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 吧。"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章