How to cast custom type slice to primitive slice in Golang?

Issue

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.

Answered By – Emil

Answer Checked By – Senaida (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.