dongleibeng5602 2014-04-05 18:57
浏览 33
已采纳

在Google App Engine中将reflect.Value传递到datastore.GetMulti

I have a wrapper function mypkg.GetStart around datastore.GetMulti. The arguments of the wrapper function must be identical to appengine.GetMulti. I would like to get the first two entities of dst, for the sake of this example. My code currently looks like below but does not work. datastore.GetMulti produces the error datastore: dst has invalid type.

type myEntity struct {
    Val Int
}

keys := []*datastore.Key{keyOne, keyTwo, keyThree}
entities := make([]myEntity, 3)
mypkg.GetStart(c, keys, enities)

My mypkg.GetStart code is as follows:

func GetStart(c appengine.Context, keys []*datastore.Key, dst interface{}) error{
    v := reflect.ValueOf(dst)
    dstSlice := v.Slice(0, 2)
    return datastore.GetMulti(c, keys, dstSlice)
}

How can I make this work? Note this is a follow up question to How to sub slice an interface{} that is a slice?

  • 写回答

1条回答 默认 最新

  • drsl90685154 2014-04-05 19:55
    关注

    I got this to work by adding Interface() to dstSlice:

    func GetStart(c appengine.Context, keys []*datastore.Key, dst interface{}) error{
        v := reflect.ValueOf(dst)
        dstSlice := v.Slice(0, 2)
        return datastore.GetMulti(c, keys, dstSlice.Interface())
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错