define generic type by using interface in golang
1.泛型起點(diǎn): interface contains type constraints
在 Go 1.18 版本中坟比,泛型被引入。
在之前的 Go 版本中,interface只是方法的集合想罕,但在 1.18 版本中惊奇,為了引入泛型而擴(kuò)展了原有interface的使用方式牲芋,從此interface可以額外表達(dá)【類型限制】的語(yǔ)義耍共。
interface can contain Type Constraints
2.interface before Go 1.18 without Type Constraints
Go 1.18 前的 interface 是鴨子類型(duck typing)的一種具體實(shí)踐乃沙,實(shí)現(xiàn)了 Go 風(fēng)格的 Duck typing诽表。(注:python大量的鴨子類型唉锌,而不需要定義接口。不同語(yǔ)言中鴨子類型的實(shí)現(xiàn)不完全相同竿奏。golang中使用interface實(shí)現(xiàn)鴨子類的方式非常顯式和直接)
interface袄简,或者說(shuō)鴨子類型的關(guān)注點(diǎn)在于對(duì)象的行為,能做什么泛啸;而不是關(guān)注對(duì)象所屬的類型
因此绿语,an interface only defines a set of methods
interface本身就是廣義的泛型,是不限制具體實(shí)現(xiàn)的泛型
3.interface after Go 1.18 with Type Constraints
為了引入泛型候址,Go 1.18 后的interface不僅定義行為吕粹,還能夠限制接口的實(shí)現(xiàn),也就是限制具體實(shí)現(xiàn)所屬的類型
interface 成為【可以限制具體實(shí)現(xiàn)】的泛型岗仑,即interface can contain Type Constraints
eg.
type I struct {
int | int32 | float64 // 限制接口I的實(shí)現(xiàn)必須是 int | int32 | float64 的實(shí)例對(duì)象
}
注意匹耕,實(shí)際上,接口應(yīng)當(dāng)只定制協(xié)議荠雕,而不應(yīng)該去限制具體實(shí)現(xiàn)稳其。帶有 Type Constraints 的 interface 讓接口的語(yǔ)義變得模糊不清驶赏,將 Type Constraints 納入 interface 未必是一個(gè)好的設(shè)計(jì)。你見(jiàn)過(guò)要限制品牌既鞠、材質(zhì)的充電器的接口協(xié)議么母市?
于是,golang對(duì)于帶/不帶 Type Constraints 的 interface 的使用場(chǎng)景也是有區(qū)別的损趋,帶有 Type Constraints 的 interface 只能用作泛型的類型限制患久,保證類型參數(shù)的值必須在特定范圍內(nèi)
帶有 Type Constraint 的 interface 只能用作泛型中的類型限制。從這種意義上看浑槽,interface with Type Constraint 可以認(rèn)為是新的類型
// You can edit this code!
// Click here and start typing.
// https://go.dev/play/p/4Z84Y-SPFqg
package main
import "fmt"
type IWithTypeConstraint interface {
~int32 | ~int64
}
type IWithoutTypeConstraint interface {
}
func main() {
// var i IWithTypeConstraint = int32(1) // compile failed. cannot use type IWithTypeConstraint outside a type constraint: interface contains type constraints
// fmt.Println(i)
var i IWithoutTypeConstraint = int32(1)
fmt.Println(i)
}
4.泛型的設(shè)計(jì)
- Go的泛型實(shí)現(xiàn)的是類型參數(shù)化蒋失。泛型通過(guò)方括號(hào)
[]
定義類型參數(shù)(類型參數(shù)和一般參數(shù)一樣,都是參數(shù)桐玻,只是用途不同)在Go語(yǔ)言的泛型方案中篙挽,泛型函數(shù) 和 泛型類型 都需要顯式地聲明類型參數(shù)(type parameter),并向類型參數(shù)傳參以實(shí)例化 泛型函數(shù) 和 泛型類型 - 在編譯時(shí)镊靴,Go編譯器會(huì)為每個(gè)實(shí)際使用的泛型類型生成特定的代碼铣卡,從而在運(yùn)行時(shí)避免額外的類型檢查和類型轉(zhuǎn)換開(kāi)銷(xiāo)
5.泛型的語(yǔ)法
[TypeParameter typeConstraint]
eg [T Any]
首先需要定義泛型參數(shù),如 T
偏竟, 在結(jié)構(gòu)或函數(shù)名稱后面方括號(hào)里面使用煮落。T
可以是任何類型或者接口,即[T]
實(shí)際上是[T Any]
type Node[T] struct {
value T
}
T
可以加約束條件:
type parameter with constraints
type Node[T comparable] struct {
value T
}
You can declare your costom type constraint as an interface.
If you declare a type constraint interface with three methods, then use it with a type parameter in a generic function, type arguments used to call the function must have all of those methods.
Constraint interfaces can also refer to specific types
類型限制是泛型的一部分踊谋,它允許您限制泛型類型參數(shù)可以接受的具體類型蝉仇。這可以幫助您編寫(xiě)更加安全、可預(yù)測(cè)的泛型代碼殖蚕。在使用類型限制時(shí)轿衔,您可以指定一個(gè)接口作為限制,泛型類型參數(shù)必須滿足這個(gè)接口睦疫。這意味著害驹,泛型類型參數(shù)必須實(shí)現(xiàn)這個(gè)接口所定義的所有方法。
// https://go.dev/play/p/LpSyeLQmhkd
package main
import "fmt"
type Addable interface {
// define generic type constraints by using interface
~int | ~float64
}
func PrintSum[T Addable](a, b T) {
fmt.Println(a + b)
}
func main() {
PrintSum(1, 2) // 輸出 3
PrintSum(1.1, 2.2) // 輸出 3.3
}
總結(jié)
An interface without type constraints, only defines a set of methods, is a pure interface, and is the protocol itself
Interfaces without type constraints, not pure interfaces, should be treated as new types for generics