Home > other >  Invalid type assertion by pointer to array
Invalid type assertion by pointer to array

Time:02-27

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)

CodePudding user response:

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)
  •  Tags:  
  • go
  • Related