在Google App Engine中传递值反映为数据存储.GetMulti [英] Passing reflect.Value to datastore.GetMulti in Google App Engine

查看:143
本文介绍了在Google App Engine中传递值反映为数据存储.GetMulti的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包装函数 mypkg.GetStart 围绕 datastore.GetMulti 。包装函数的参数必须与 appengine.GetMulti 相同。为了这个例子,我想得到 dst 的前两个实体。我的代码目前看起来像下面但不起作用。 datastore.GetMulti 产生错误数据存储:dst具有无效类型

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)

我的 mypkg.GetStart 代码如下:

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?

推荐答案

我通过添加 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())
}

这篇关于在Google App Engine中传递值反映为数据存储.GetMulti的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆