Assuming only arrays are passed as arguments to the arr
parameter, I would like each call of unpackArray()
to return the argument casted from its original array type to type []any
.
package main
func unpackArray(arr any) []any {
return arr.([]any)
}
func main() {
myArr1 := []string {"Hey"}
myArr2 := []int {60}
unpackArray(myArr1)
unpackArray(myArr2)
}
However, this code yields error panic: interface conversion: interface {} is []string, not []interface {}
. So it is not allowing me to cast an interface whose static type is not type []any
to type []any
.
So, given I know that arr
's static type is some type of array, and without changing the arr
parameter's initialization type from any
, how could I convert arr
to type []any
using this function?
(I am encountering the same problem with maps where I cannot cast from an arbitrary map type to type map[any]any
, but I am guessing the solution to this issue would be similar to the solution for arrays.)