英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论