Go 泛型是什么?Go 1.18 新特性
Go 1.18 正式引入泛型:函数和类型可带类型参数。语法:func Max[T comparable](a, b T) T {};约束用 interface(any、comparable、~int、int | float64 类型集);类型参数可用于结构体:type Stack[T any] struct { items []T }。常用场景:通用容器、工具函数(Map/Filter/Reduce)、避免 interface{} + 类型断言的样板代码。注意:泛型不能用于方法,Go 1.21+ 提供标准泛型包 slices、maps。
一起交流
分享经验,让问题更进一步
讨论,从你的观点开始
还没有回复,欢迎补充经验或分享不同的思路。