dimanche 17 avril 2022

Get value of interface{ } without having access to type

How can I get the value of x as []interface{} ?

func main() {

    var x interface{} = SomeFunc()
    
    fmt.Println(x) // this prints [1 2].

    val := x.([]interface{}) // this will not work because '[]interface{}' is different type than 'a'

}

func SomeFunc() interface{} {
    // some type I dont have access to
    type a []interface{}

    var someArray a = make([]interface{}, 0)
    someArray = append(someArray, 1)
    someArray = append(someArray, 2)
    return someArray
}





Aucun commentaire:

Enregistrer un commentaire