goslicecustom-type

How to cast custom type slice to primitive slice in 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?


Solution

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