vendredi 22 octobre 2021

Dereference eventual pointers in an empty interface array [duplicate]

I have an array of interface{}, and some of the elements may be pointers. I want to "flatten" the array such that no element is a pointer, so I want to dereference eventual pointers. Essentially I want to do something like that :

maybePointers := []interface{}{}
noPointers := []interface{}{}
var val interface{}
for _,v := range maybePointers {
    
    // if v is a pointer, dereference it
    reflectVal := reflect.ValueOf(v)
    if reflectVal.Kind() == reflect.Ptr {
        v = *v.(*interface{})
    }
    noPointers = append(noPointers, v)
}

But with this code, I get interface conversion: interface {} is *int64, not *interface {}





Aucun commentaire:

Enregistrer un commentaire