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?


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

Answered By – Emil

Answer Checked By – Senaida (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.