wx

关注公众号领取Go学习资料

wx

洋葱小店

wx

LUCA

Go语言泛型入门


Go语言泛型入门 Go语言在 1.18中引入了对使用参数化类型的泛型代码的新支持。泛型是一种编写代码的方式,与所使用的特定类型无关。函数和类型现在可以被编写为使用一组类型中的任何一个。本课展示了在代码中使用泛型的一些示例。

类型参数泛型

Go函数可以使用类型参数让函数支持多个类型。函数的类型参数用中括号包裹,位于函数的参数之前。

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

这个声明意味着s是任何类型T的一个切片,它满足comparable内置约束。x也是相同类型的值。 compatible是一个有用的约束,可以使用==和!=类型值上的运算符。在本例中,我们使用它将一个值与所有切片元素进行比较,直到找到匹配。此Index函数适用于任何支持比较的类型。

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"))
}

泛型类型

除了泛型函数外,Go还支持泛型类型。可以使用类型参数对类型进行参数化,这对于实现泛型数据结构非常有用。此示例演示了一个简单的类型

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() {
}

声明类型约束

我们可以将类型约束声明为接口。约束允许实现接口的任何类型。,以下代码声明类型约束:

type Number interface {
    int64 | float64
}

这样,类型参数约束为int64或float64时,可以使用此Number类型约束,而不是写出int64|float64。下面使用Number类型参数实现一个SumNumber函数

func SumNumbers[K comparable, V Number](m map[K]V) V {
    var s V
    for _, v := range m {
        s += v
    }
    return s
}

练习

实现一个泛型版求较小值的函数Min(),要求支持 int int8 int32 int64四种类型; 参考代码见Github

如有疑问关注公众号给我留言
wx

关注公众号领取Go学习资料

wx

洋葱小店

wx

LUCA

©2017-2023 鲁ICP备17023316号-1 Powered by Hugo