云数据存储查询,关键在哪里?

huangapple go评论155阅读模式
英文:

Cloud datastore queries, where is the key?

问题

我在Google云数据存储中存储了一些数据。

查询数据不是问题,我可以使用迭代器并获取数据的属性。
示例:
https://cloud.google.com/datastore/docs/concepts/queries#projection_queries

var priorities []int
var percents []float64
it := client.Run(ctx, query)
for {
var task Task
if _, err := it.Next(&task); err == iterator.Done {
break
} else if err != nil {
log.Fatal(err)
}
priorities = append(priorities, task.Priority)
percents = append(percents, task.PercentComplete)
}

我可以轻松访问实体的属性,但不知道如何读取/访问键。

如何获取键?

英文:

I have stored some data on Google cloud datastore.

Querying the data is not an issue, I can use an iterator and get the properties of the data.
example;
https://cloud.google.com/datastore/docs/concepts/queries#projection_queries

var priorities []int
var percents []float64
it := client.Run(ctx, query)
for {
    var task Task
    if _, err := it.Next(&task); err == iterator.Done {
            break
    } else if err != nil {
            log.Fatal(err)
    }
    priorities = append(priorities, task.Priority)
    percents = append(percents, task.PercentComplete)
}

I can access the Properties of the entity with no problem but have no idea on how to read/access the keys.

How do I get the keys?

答案1

得分: 2

你可以在这里看到,当调用Next时,迭代器会返回相关联的键。在上面的示例中,这并不需要,因此通过使用空白标识符_, err := it.Next(&task)将其丢弃。如果你想要键,则不要丢弃它:

for {
    var task Task
    key, err := it.Next(&task)
    if err != nil && err != iterator.Done {
         return err
    } else if err == iterator.Done {
         break
    }
    priorities = append(priorities, task.Priority)
    percents = append(percents, task.PercentComplete)

    // 使用键做一些操作
}
英文:

You can see here that the iterator returns the associated key when calling Next. In the example above it is not needed and is therefore discarded by using the blank identifier, i.e. _, err := it.Next(&task). If you want the key then do not discard it:

for {
    var task Task
    key, err := it.Next(&task)
    if err != nil && err != iterator.Done {
         return err
    } else if err == iterator.Done {
         break
    }
    priorities = append(priorities, task.Priority)
    percents = append(percents, task.PercentComplete)

    // do something with key
}

huangapple
  • 本文由 发表于 2021年11月30日 14:38:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/70165272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定