例如,有 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
}
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\n",
SumIntsOrFloats[string, int64](ints),
SumIntsOrFloats[string, float64](floats))
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\n",
SumIntsOrFloats[string, int64](ints),
SumIntsOrFloats[int32, float64](floats))
即:需要和函数定义一样写明参数类型。
当然,此处参数类型不写也可以(可以省略,但前提是编译器可以从函数参数的类型推断类型参数):
fmt.Printf("Generic Sums, type parameters inferred: %v and %v\n",
SumIntsOrFloats(ints),
SumIntsOrFloats(floats))
类型约束可以定义为接口,可以简化代码,便于复用:
上述代码可进一步简化为:
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\n",
SumNumbers(ints),
SumNumbers(floats))
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\n",
SumIntsOrFloats[string, int64](ints),
SumIntsOrFloats[int32, float64](floats))
fmt.Printf("Generic Sums: %v and %v\n",
SumIntsOrFloats(ints),
SumIntsOrFloats(floats))