// Numeric expresses a type constraint satisfied by any numeric type. type Numeric interface { uint | uint8 | uint16 | uint32 | uint64 | int | int8 | int16 | int32 | int64 | float32 | float64 | complex64 | complex128 }
// Sum returns the sum of the provided arguments. funcSum[TNumeric](args ...T)T { var sum T for i := 0; i < len(args); i++ { sum += args[i] } return sum }
二、波浪线前缀在约束中表示支持同基础类型的其它类型。在定义上述内容的前提下,编写以下代码:
1 2 3 4 5 6
// id is a new type definition for an int64 type id int64
funcmain() { fmt.Println(Sum([]id{1, 2, 3}...)) }
编译将会报错:
id does not implement Numeric (possibly missing ~ for int64 in constraint Numeric)
需在类型集合的int64部分加入~前缀。
三、当某类型中包含泛型时,泛型符号必须被包含在函数接收者中。如有以下泛型类型:
1 2 3 4 5 6 7 8 9 10 11 12 13
// Ledger is an identifiable, financial record. type Ledger[T ~string, K Numeric] struct {
// ID identifies the ledger. ID T
// Amounts is a list of monies associated with this ledger. Amounts []K
// SumFn is a function that can be used to sum the amounts // in this ledger. SumFn SumFn[K] }
为其定义方法时,需在函数接收者部分显式包含泛型符号:
1 2 3 4 5
// PrintIDAndSum emits the ID of the ledger and a sum of its amounts on a // single line to stdout. func(l Ledger[T, K])PrintIDAndSum() { fmt.Printf("%s has a sum of %v\n", l.ID, l.SumFn(l.Amounts...)) }