如何在 Golang 中将自定义类型切片转换为原始切片?
How to cast custom type slice to primitive slice in Golang?
type TCustomIntType int
func aFunc() {
var fails []TCustomIntType = []TCustomIntType([]int{})
}
我得到了:
cannot convert []int literal (type []int) to type []TCustomIntType
如何解决?我必须手动编写转换函数吗?
是的,您必须使用 for 循环手动复制它。
type TCustomIntType int
func aFunc() {
var fails []TCustomIntType = []TCustomIntType([]int{})
}
我得到了:
cannot convert []int literal (type []int) to type []TCustomIntType
如何解决?我必须手动编写转换函数吗?
是的,您必须使用 for 循环手动复制它。