【发布时间】:2022-11-29 12:11:26
【问题描述】:
我正在尝试使用泛型构建一个函数,它将接口片段转换为 T 类型片段。
我想出了以下:
func convertInterfaceArray[T any](input []any, res []T) {
for _, item := range input {
res = append(res, item.(reflect.TypeOf(res[0])))
}
}
但是,这不会编译。但是你明白了。 T 可以是任何类型,我有一个 []any 类型的输入切片需要转换为 []T
【问题讨论】: