Invalid type assertion by pointer to array

Issue

I’m new to golang and confused about the type assertion. Why can’t the following snippet be compiled? what could be the problem by the type assertion in this example?

arr := new([5]int)
arr1, ok := arr.(*[5]int)

Solution

type assertion is only for interface.

A type assertion provides access to an interface value’s underlying concrete value.

source https://go.dev/tour/methods/15

example:

    arr := new([5]int)
    i := interface{}(arr)
    arr1, ok := i.(*[5]int)
    fmt.Println(arr1, ok)

Answered By – Rahmat Fathoni

Answer Checked By – Mildred Charles (GoLangFix Admin)

Leave a Reply

Your email address will not be published.