【发布时间】:2018-09-11 21:54:40
【问题描述】:
datastore.Entity 结构看起来非常有用,这就是我想要处理实体的方式,但我没有看到任何使用它的 API。大多数函数(例如Get)采用interface{},只有当它是精确与传入数据结构类似的结构时,它似乎才有效。
// https://godoc.org/cloud.google.com/go/datastore#Client.Get
ctx := context.Background()
client, err := datastore.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
type Article struct {
Title string
Description string
Body string `datastore:",noindex"`
Author *datastore.Key
PublishedAt time.Time
}
key := datastore.NameKey("Article", "articled1", nil)
article := &Article{}
if err := client.Get(ctx, key, article); err != nil {
// TODO: Handle error.
}
我如何以广义的方式获得这个实体?如果我不完全了解结构怎么办? (更具体地说,如何获取datastore.Entity 的实例?)
【问题讨论】: