云数据存储查询,关键在哪里?
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)
}
我可以毫无问题地访问实体的属性,但不知道如何 read/access 键。
如何获得钥匙?
可以看到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,即_, 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)
// do something with 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)
}
我可以毫无问题地访问实体的属性,但不知道如何 read/access 键。
如何获得钥匙?
可以看到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,即_, 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)
// do something with key
}