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

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

报告相同问题?