dongzhi4239 2015-03-27 16:42
浏览 37
已采纳

在Golang的Google数据存储区中使用任意键数组进行查询

A continuation from this question:

Doing a "IN Array" query on google app engine datastore with golang

Right now, I am following the suggestion from the previous question on querying with an array of keys/ids ids []int64. These IDs may or may not actually exist (they have been deleted, but the reference on other instances have not been removed).

My method of trying to obtain these instances looks like so:

var keys []*datastore.Key

for _, id := range ids {
    keys = append(keys, datastore.NewKey(c, "Category", "", id, nil))
}

categories := make([]Category, len(keys))
err := datastore.GetMulti(c, keys, categories)
if err != nil {
    return nil, err
}

for i := 0; i < len(categories); i++ {
    categories[i].Id = keys[i].IntID()
}

However, it errors out throwing me:

datastore: no such entity

I could on the other hand grab each one individually, but is there a more efficient way to approach this?

  • 写回答

1条回答 默认 最新

  • dongyuqie4322 2015-03-27 17:37
    关注

    You need to type assert the error to an appengine.MultiError. This way you can get access to the errors for an individual entity.

    if me, ok := err.(appengine.MultiError); ok {
        for i, e := range me {
            // e != nil if entity i failed
        }
    } else {
       // something else went wrong (timeout, etc).
    }
    

    See the docs for MultiError here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路