如何在Golang中将自定义类型切片转换为原始切片?

如何在Golang中将自定义类型切片转换为原始切片?

问题描述:

type TCustomIntType int

func aFunc() {
    var fails []TCustomIntType = []TCustomIntType([]int{})
}

I got:

cannot convert []int literal (type []int) to type []TCustomIntType

How to fix it? Do I have to write a convert func manually?

  type TCustomIntType int 
 
func aFunc(){
 var failed [] TCustomIntType = []  TCustomIntType([[int {})
} 
  code>  pre> 
 
 

我得到了: p>

无法转换[] int 文字(类型[] int)键入[] TCustomIntType code> p>

如何解决? 我必须手动编写转换函数吗? p> div>

Yes, you have to copy it manually with a for loop.