Go 1.27 即將支援 Generic Method:告別 package-level 函式的 workaround

Go 1.27 即將支援 Generic Method:告別 package-level 函式的 workaround

前言 Go 1.18 帶來泛型之後,我們終於可以寫出型別安全的通用函式,不再需要到處 interface{} 加型別斷言。但一個眾所周知的限制一直存在到現在,method 沒辦法有自己的 type parameter。 換句話說,這段程式碼在目前的 Go 版本中是非法的: func (c *Client) doRequest[T any](ctx context.Context, method, path string, body any) (*T, error) { // 編譯錯誤:cannot use generic method doRequest without instantiation } 為了繞過這個限制,大家只能退而求其次,把 receiver 當作普通參數傳進去,改用 package-level 的 generic function。 最近 Go issue #77273 的進展讓這件事有了轉機,generic method 的設計已被接受,預計在 Go 1.27 正式釋出。 為什麼現在沒有 Generic Method? 這不是 Go team 沒想到,而是刻意的設計決策。 Go 的 interface 是核心功能,method 必須能夠被 dispatch 到具體的實作上。如果 method 自身帶有 type parameter,編譯器在 dispatch 時需要知道 T 是什麼,這在動態 dispatch(interface dispatch)的情境下是個難題,runtime 無法事先知道要生成哪個型別的實例化版本。 所以泛型剛推出時,generic method 被刻意排除在外,等待一個比較完整的設計方案。這個等待從 Go 1.18 一路等到了 1.27(暫定)。 ...