[英]Reading data from Google Could Datastore on AppEngine with Go
Here is a screen shot of the entities I'm trying to read. 这是我正在尝试阅读的实体的屏幕截图。
Here is my go code: 这是我的代码:
package readfromgcd
import (
"net/http"
"appengine"
"appengine/datastore"
"fmt"
)
type person struct {
firstname string
lastname string
}
func init () {
http.HandleFunc("/", readpeople)
}
func readpeople (w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
q := datastore.NewQuery("person")
people := make([]person, 0, 20)
if _, err := q.GetAll(c, &people); err !=nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
fmt.Fprint(w, "Hello world!")
}
I get the following result: datastore: cannot load field "firstName" into a "readpeople.person": no such struct field 我得到以下结果:datastore:无法将字段“firstName”加载到“readpeople.person”中:没有这样的struct字段
Here is a screenshot. 这是一个截图。 result 结果
This code does not show doing anything with this data. 此代码不显示对此数据执行任何操作。 I wanted to limit this post to the retrieval. 我想将此帖限制为检索。 I must be missing something simple. 我一定很遗憾。 Where have I gone wrong? 我哪里出错了? Thanks in advance for any help. 在此先感谢您的帮助。
Property names in your datastore do not match field names in your Go struct person
. 数据存储区中的属性名称与Go结构person
中的字段名称不匹配。 For example in your datastore person
has property firstName
but your struct has field firstname
. 例如,在您的数据存储区中, person
具有属性firstName
但您的struct具有字段firstname
。
So first thing is to make them match; 所以首先要让它们匹配; or if you want to use different names in your Go struct, use struct tags to define the mappings. 或者如果要在Go结构中使用不同的名称,请使用struct标签来定义映射。
Another important thing: you have to export your type and its fields in order so that the datastore
package will be able to load data into it using reflection. 另一个重要的事情是:您必须按顺序导出类型及其字段,以便datastore
包能够使用反射将数据加载到其中。 So you have to start your type name and its fields with uppercase letters: 因此,您必须使用大写字母开始您的类型名称及其字段:
type Person struct {
FirstName string `datastore:"firstName"`
LastName string `datastore:"lastName"`
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.