泛型

Generics

Type parameters 类型参数

原文:https://go.dev/tour/generics/1

​ 可以使用类型参数编写 Go 函数,以便对多种类型进行操作。一个函数的类型参数出现在括号中,在函数的参数之前。

1
func Index[T comparable](s []T, x T) int

​ 这个声明意味着s是任何类型T的一个切片,满足内置的comparable约束。x也是同一类型的值。

comparable是一个有用的约束,它使我们可以在该类型的值上使用==!=运算符。在这个例子中,我们用它来比较一个值和所有的切片元素,直到找到一个匹配。这个Index函数适用于任何支持比较的类型。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main

import "fmt"

// Index returns the index of x in s, or -1 if not found.
func Index[T comparable](s []T, x T) int {
	for i, v := range s {
		// v and x are type T, which has the comparable
		// constraint, so we can use == here.
		if v == x {
			return i
		}
	}
	return -1
}

func main() {
	// Index works on a slice of ints
	si := []int{10, 20, 15, -10}
	fmt.Println(Index(si, 15))

	// Index also works on a slice of strings
	ss := []string{"foo", "bar", "baz"}
	fmt.Println(Index(ss, "hello"))
}

Generic types 范型

原文:https://go.dev/tour/generics/2

​ 除了泛型函数,Go 还支持泛型。一个类型可以用一个类型参数进行参数化,这对实现通用数据结构很有用。

​ 这个例子演示了一个简单的类型声明,用于保存任何类型值的单链式列表。

作为练习,为这个列表的实现添加一些功能。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package main

// List represents a singly-linked list that holds
// values of any type.
type List[T any] struct {
	next *List[T]
	val  T
}





func main() {
}

Congratulations!

原文:https://go.dev/tour/generics/3

您完成了这一课!

您可以回到模块列表中寻找下一步要学习的内容,或者继续学习下一课。

最后修改 February 5, 2024: 更新 (f57b279)