如何将接口转换为接口切片?

My input is an interface{}, and I know it can be an array of any type.

I'd like to read one of the elements of my input, so I try to convert my interface{} into an []interface{}, but go will give me the following error:

panic: interface conversion: interface {} is []map[string]int, not []interface {}

How can I do that conversion? (without reflect if possible).

Playground test

Thanks

I think what you are looking is type assertion

package main

import (
    "fmt"
)

func main() {

    parentStruct := map[string]interface{}{
         "test": []map[string]int{
          {"a": 1, "b": 2},
          {"c": 3},
        },
   }

   testStruct := parentStruct["test"].([]map[string]int)

   fmt.Println(testStruct)
}

read this link: https://golang.org/ref/spec#Type_assertions

https://play.golang.org/p/81uL2hgrN3l

The point of the interface is to define the behaviour you want to use, if you use an empty interface, you know nothing about the types in that slice.

If you want to print it, you can use println or printf with no conversion.

If you want to access it, and must allow any type, you can use reflect (slow and complex to use).

If you want to acess it, and use common behaviour/ data that you can define functions for, define an interface, e.g. :

type Doer interface {
 Do() error
}
parentStruct := []Doer{...}
testStruct.Do()

If none of that works, wait for Go 2 and generics.