我使用反射在转到动态地从缓存中获取数据到各种静态声明的结构类型时遇到问题:
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
不会这个静态类型的缓存中的数据转换回一个体现价值,而是返回此错误: gob: local interface type *interface {} can only be decoded from remote interface type; received concrete type
gob: local interface type *interface {} can only be decoded from remote interface type; received concrete type
...:\
该数据在代码中缓存保存在别处,而不需要reflect
。