samedi 28 mai 2016

Issue translating data back from serialization into Go struct dynamically using reflection

I'm having trouble using reflection in Go to fetch data from a cache dynamically into various statically declared struct types:

func FetchFromCacheOrSomewhereElse(cacheKey string, returnType reflect.Type) (out interface {}, err error) {
    fetchFromCache := reflect.New(returnType).Interface();
    _, err=memcache.Gob.Get(*context, cacheKey, &fetchFromCache);

    if (err==nil) {
        out=reflect.ValueOf(fetchFromCache).Elem().Interface();

    } else if (err==memcache.ErrCacheMiss) {
        /* Fetch data manually... */

    }

    return out, err;

}

Reflect won't translate this statically typed cache data back into a reflect value, and returns this error instead: gob: local interface type *interface {} can only be decoded from remote interface type; received concrete type ... :\

This data is saved elsewhere in the code to the cache without the need for reflect.





Aucun commentaire:

Enregistrer un commentaire