go - 从字符串制作 Google App Engine 数据存储区 key

标签 go google-cloud-datastore

我在 URL 上将实体“id”作为字符串传递,例如:

..../foo/234565

我想在以下查询中使用 id:

//...i hace some code that gets stringId from the URL, and I verified that it works
stringId = ....
theKey, err := datastore.DecodeKey(stringId)
q := datastore.NewQuery("Foo").Filter("__key__ =", theKey)

我得到的错误:

proto: can't skip unknown wire type 7 for datastore.Reference

有没有一种简单的方法可以将 stringId 转换为“Key”?

最佳答案

c := appengine.NewContext(r)
//Retrieve the entity ID from the submitted form. Convert to an int64
entity_id := r.FormValue("entity_id")
entity_id_int, err := strconv.ParseInt(entity_id, 10, 64) 
if err != nil {
  fmt.Fprint(w, "Unable to parse key")
  return;
}
//We manufacture a datastore key based on the Kind and the 
//entity ID (passed to us via the HTTP request parameter.
key := datastore.NewKey(c, kind, "", entity_id_int, nil)
//Load the Entity this key represents.
//We have to state the variable first, then conduct the Get operation 
//so the datastore understands the struct representing the entity.
var entity CustomStruct
datastore.Get(c, key, &entity)

关于go - 从字符串制作 Google App Engine 数据存储区 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26593806/

相关文章:

java - 使用Objectify在GAE上并发写入数据

pointers - 映射的键作为结构体的指针? (用例)

go - 使用 golang 将 DateTime 值插入 MS SQL

google-cloud-datastore - 克隆 NDB 实体的最佳方法是什么?

java - 从数据存储中检索实体的时间

python - 无法从 google.cloud 导入数据存储

go - 如果一个 slice 改变,两个 slice 都会改变

go - 优化网络代码

go - 如何检查文件是否是 GoLang 中的 tar 文件?

java - 澄清具有祖先和一个属性的复合索引