go在1.18之后增加了泛型的设置,这一重大改变,为我们在工作避免的很多麻烦,可以使我们开发者在函数或类型定义中使用多种类型来定义,下面用一个例子来带领大家进入到go泛型的世界
func execFunc[T string | int | float64](name T) (age T) {
fmt.Println(name)
return name
}
func TestGoCountDown(t *testing.T) {
execFunc(1.111)
}
go在1.18之后增加了泛型的设置,这一重大改变,为我们在工作避免的很多麻烦,可以使我们开发者在函数或类型定义中使用多种类型来定义,下面用一个例子来带领大家进入到go泛型的世界
func execFunc[T string | int | float64](name T) (age T) {
fmt.Println(name)
return name
}
func TestGoCountDown(t *testing.T) {
execFunc(1.111)
}