使用Go获取Google的Datastore键值对

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

Getting Google's datastore Key value with Go

问题

我有一个结构体:

type struct Foo {
    Id ??
    Name string
}

我通过以下方式获取了一个Foo的列表,其中"c"是一个NewContext():

q := datastore.NewQuery("Drug")
var foos []Foo
_, err := q.GetAll(c, &foos)

Id没有被填充,但是Name被填充了。
我正在尝试找出如何填充Id。

英文:

I have a struct:

type struct Foo {
    Id ??
    Name string
}

I get a list of Foos doing, where "c" is a NewContext():

	q := datastore.NewQuery("Drug")
	var foos []Foo
	_, err := q.GetAll(c, &foos)

The Id is not populated, but the Name is.
I'm trying to figure out how to get the Id to populate

答案1

得分: 0

GetAll 函数返回一个键的切片:

keys, err := q.GetAll(c, &foos)

foos[i] 的键是 keys[i]

如果需要,可以使用循环将 ID 存储在实体中:

for i := range keys {
   foos[i].ID = keys[i].IntID() // 或者 .StringID()
}
英文:

The GetAll function returns a slice of the keys:

keys, err := q.GetAll(c, &foos)

The key for foos[i] is keys[i].

Use a loop to store the id in the entity if that's what you need:

for i := range keys {
   foos[i].ID = keys[i].IntID() // or .StringID()
}

huangapple
  • 本文由 发表于 2014年10月28日 00:36:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/26592362.html
匿名

发表评论

匿名网友

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

确定