golang泛型快速入门使用(go 1.18及以后版本)
1、上手使用
例如,有 string->int64 和 string->float64 两类 map,我们需要对 map 的 value 求和。 使用泛型前的代码:
// SumInts adds together the values of m. func SumInts(m map[string]int64) int64 { var s int64 for _, v := range m { s += v } return s } // SumFloats adds together the values of m. func SumFloats(m map[string]float64) float64 { var s float64 for _, v := range m { s += v } return s }
使用泛型替换上述代码:
// SumIntsOrFloats sums the values of map m. It supports both int64 and float64 // as types for map values. func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V { var s V for _, v := range m { s += v } return s }
代码说明:
-
声明了一个 SumIntsOrFloats 函数,此函数参数类型为 map,返回值类型为 V,函数的作用是对 map 的 int64 或 float64 类型 value 求和。 map 的 K 和 V 是泛型类型,泛型的约束写在 方括号 [ ] 中,K 具有约束 comparable(即可做 == 或 != 运算,参考 ),V 是 int64 或 float64(也相当于是对入参类型的约束)。 该函数的调用方式:
ints := map[string]int64{ "a":1, "b":2, } floats := map[string]float64{ "a":3.0, "b":4.0, } fmt.Printf("Generic Sums: %v and %v ", SumIntsOrFloats[string, int64](ints), SumIntsOrFloats[string, float64](floats))
-
当然,map 的 key 的类型也可以不一样,满足
ints := map[string]int64{ "a":1, "b":2, } floats := map[int32]float64{ 5:3.0, 6:4.0, } fmt.Printf("Generic Sums: %v and %v ", SumIntsOrFloats[string, int64](ints), SumIntsOrFloats[int32, float64](floats))
即:需要和函数定义一样写明参数类型。 当然,此处参数类型不写也可以(可以省略,但前提是编译器可以从函数参数的类型推断类型参数):
fmt.Printf("Generic Sums, type parameters inferred: %v and %v ", SumIntsOrFloats(ints), SumIntsOrFloats(floats))
-
“编译器可以从函数参数的类型推断类型参数”,不太明白,哪位大佬举个例子或给个反例?
2、关于类型约束
类型约束可以定义为接口,可以简化代码,便于复用: 上述代码可进一步简化为:
type Number interface { int64 | float64 } // SumNumbers sums the values of map m. It supports both integers // and floats as map values. func SumNumbers[K comparable, V Number](m map[K]V) V { var s V for _, v := range m { s += v } return s } fmt.Printf("Generic Sums with Constraint: %v and %v ", SumNumbers(ints), SumNumbers(floats))
3、完整代码
package main import "fmt" func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V { var s V for _, v := range m { s += v } return s } func main() { ints := map[string]int64{ "a":1, "b":2, } floats := map[int32]float64{ 5:3.0, 6:4.0, } fmt.Printf("Generic Sums: %v and %v ", SumIntsOrFloats[string, int64](ints), SumIntsOrFloats[int32, float64](floats)) fmt.Printf("Generic Sums: %v and %v ", SumIntsOrFloats(ints), SumIntsOrFloats(floats))