今天继续学习Go 1.18引入的泛型,每天利用几分钟的时间来学习,慢慢积累。

前两天学习了在Go中如何定义泛型函数,并学习了Go泛型中的类型约束(Type Constrant)的概念,今天通过参考Go官方的泛型技术提案文档,简单整理一下Go泛型的基本语法。

Go的泛型指的是在Go的类型声明和函数声明中增加可选的类型参数。类型参数要受到类型约束,Go使用嵌入额外元素的接口类型来定义类型约束,类型约束定义了一组满足约束的类型集(Type Set)。

类型参数(Type Parameter)

Go泛型代码可以在Go的函数声明或类型声明中增加类型参数列表,而在类型参数列表中声明的类型名称就是类型参数。

类型参数在声明中充当了一个未知类型的占位符,在泛型函数或泛型类型被具化(instantiation)时,类型参数将会被"类型实参"所替换。

在函数声明中,可以在函数名称的后边,使用中括号声明一个额外的类型参数列表,例如 func F[T any](p T) { ... }。声明的类型参数可以在函数的参数和函数体中使用。 在这个例子中,T是类型参数的名字,也就是类型,any是类型参数的约束,是对类型参数可选类型的约束。但是T的类型要等到泛型函数具化时才能确定。

在类型声明中,可以在类型名称的后边,使用中括号声明一个额外的类型参数列表,例如:

1type M[T any] []T
2
3type Stack[T any] struct {
4	eles []T
5}

类型约束(Type Constraint)

每个类型参数都有一个类型约束,类型约束规定了类型参数在具化为类型实参时必须满足的条件。

Go的泛型中使用接口类型来定义类型约束。

Go 1.18内置了anycomparable两个类型约束。any表示任意类型,comparable表示类型的值可以使用==!=比较大小。 在$GOROOT/src/builtin/builtin.go中可找到它们的源码,any实际上就是interface{}的别名。

1// any is an alias for interface{} and is equivalent to interface{} in all ways.
2type any = interface{}
3
4// comparable is an interface that is implemented by all comparable types
5// (booleans, numbers, strings, pointers, channels, arrays of comparable types,
6// structs whose fields are all comparable types).
7// The comparable interface may only be used as a type parameter constraint,
8// not as the type of a variable.
9type comparable interface{ comparable }

为了支持使用接口类型来定义Go泛型类型参数的类型约束,Go 1.18对接口定义语法进行了扩展。 在接口定义中既可以定义接口的方法集(Method Set),也可以声明可以被用作泛型类型参数的类型实参的类型集(Type Set)。

 1type Constraint1 interface {
 2	T1 // 约束限制为T1类型
 3}
 4
 5type Constraint2 interface {
 6	~T1 // 约束限制为所有底层类型为T1的类型
 7}
 8
 9type Constraint3 interface {
10	T1 | T2 | T3 // 约束限制为T1, T2, T3中的一个类型
11}

注意,虽然在接口中既可以定义Method Set,也可以定义Type Set,但在定义接口时还是建议将传统的接口定义需求和用作泛型类型参数约束的需求区分开,推荐分开定义,而不要混用。

类型具化(Type Instantiation)

声明了泛型函数、泛型类型后,就可以在具体的代码中调用泛型函数和使用泛型类型。

例如,下面代码中在Go中实现了泛型的栈。

例1:

 1package main
 2
 3type Stack[T any] struct {
 4	eles []T
 5}
 6
 7func NewStack[T any]() *Stack[T] {
 8	return &Stack[T]{
 9		eles: []T{},
10	}
11}
12
13func (s *Stack[T]) IsEmpty() bool {
14	return len(s.eles) == 0
15}
16
17func (s *Stack[T]) Push(ele T) {
18	s.eles = append(s.eles, ele)
19}
20
21func (s *Stack[T]) Pop() (T, bool) {
22	size := len(s.eles)
23	if size == 0 {
24		var zero T
25		return zero, false
26	}
27	top := s.eles[size-1]
28	s.eles = s.eles[:size-1]
29	return top, true
30}
31
32func main() {
33	st := NewStack[string]()
34	st.Push("a")
35	st.Push("b")
36	st.Push("c")
37}

例1中定义了泛型类型结构体Stack[T any], T是这个泛型类型的类型参数,any是类型参数T的类型约束。

例1还定义了一个泛型函数func NewStack[T any]() *Stack[T], T是这个泛型函数的类型参数,any是类型参数T的类型约束。

在main函数中st := NewStack[string]()创建了一个Stack,就是类型的具化,泛型函数的类型参数T被"类型实参"string所替换。

例1的第24行var zero T演示了如何在泛型代码中使用泛型类型的零值。

参考