繁体   English   中英

使用Go从AppEngine上的Google Can Datastore读取数据

[英]Reading data from Google Could Datastore on AppEngine with Go

这是我正在尝试阅读的实体的屏幕截图。

实体

这是我的代码:

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!")
}

我得到以下结果:datastore:无法将字段“firstName”加载到“readpeople.person”中:没有这样的struct字段

这是一个截图。 结果

此代码不显示对此数据执行任何操作。 我想将此帖限制为检索。 我一定很遗憾。 我哪里出错了? 在此先感谢您的帮助。

数据存储区中的属性名称与Go结构person中的字段名称不匹配。 例如,在您的数据存储区中, person具有属性firstName但您的struct具有字段firstname

所以首先要让它们匹配; 或者如果要在Go结构中使用不同的名称,请使用struct标签来定义映射。

另一个重要的事情是:您必须按顺序导出类型及其字段,以便datastore包能够使用反射将数据加载到其中。 因此,您必须使用大写字母开始您的类型名称及其字段:

type Person struct {
    FirstName string `datastore:"firstName"`
    LastName string  `datastore:"lastName"`
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM