使用GAE将一个对象保存到数据存储中。当获取它时,对象的成员为空。

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

Go with GAE saved an object to datastore. when getting it back, the object has empty members

问题

我正在尝试使用GAE学习Go语言。
我创建了两个处理程序。一个用于将对象保存到数据存储中,另一个用于检索并输出到屏幕。问题是,当我从数据存储中检索UserAccount对象时,对象内的所有值都消失了。

任何帮助将不胜感激。

输出:

a/c count: 2 val: core.UserAccount{idString:"", deviceId:""} val: core.UserAccount{idString:"", deviceId:""}

type UserAccount struct {
    idString string
    deviceId string
}

func create_account(w http.ResponseWriter, r *http.Request) {

    c := appengine.NewContext(r)

        idstr := "ABCDEFGH"
        devId := r.FormValue("deviceId")

        newAccount := UserAccount{ idString: idstr, deviceId: devId,}

        key := datastore.NewIncompleteKey(c, "UserAccount", nil)
        _, err := datastore.Put(c, key, &newAccount)
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
    }

    fmt.Fprintf(w, "val: %#v \n", newAccount)
}

func get_info(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)

    q := datastore.NewQuery("UserAccount")
    accounts := make([]UserAccount, 0, 10)
    if _, err := q.GetAll(c, &accounts); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    fmt.Fprintf(w, "a/c count: %v \n", len(accounts))

    for i := 0; i < len(accounts); i++ {
        fmt.Fprintf(w, "val: %#v \n", accounts[i])
    }
}
英文:

I am trying to learn Go with GAE.
I have created 2 handlers. One for saving an object to datastore and the other retrieve it and output to screen. The problem is that when i retrieve the UserAccount object from datastore, every values inside the object are gone.

Any help would be appreciate.

Output:

a/c count: 2
val: core.UserAccount{idString:&quot;&quot;, deviceId:&quot;&quot;}
val: core.UserAccount{idString:&quot;&quot;, deviceId:&quot;&quot;}

type UserAccount struct {
    idString string
    deviceId string
}

func create_account(w http.ResponseWriter, r *http.Request) {

    c := appengine.NewContext(r)

        idstr := &quot;ABCDEFGH&quot;
        devId := r.FormValue(&quot;deviceId&quot;)

        newAccount := UserAccount{ idString: idstr, deviceId: devId,}

        key := datastore.NewIncompleteKey(c, &quot;UserAccount&quot;, nil)
        _, err := datastore.Put(c, key, &amp;newAccount)
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
    }

    fmt.Fprintf(w, &quot;val: %#v \n&quot;, newAccount)
}

func get_info(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)

    q := datastore.NewQuery(&quot;UserAccount&quot;)
    accounts := make([]UserAccount, 0, 10)
    if _, err := q.GetAll(c, &amp;accounts); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    fmt.Fprintf(w, &quot;a/c count: %v \n&quot;, len(accounts))

    for i := 0; i &lt; len(accounts); i++ {
        fmt.Fprintf(w, &quot;val: %#v \n&quot;, accounts[i])
    }
}

答案1

得分: 5

如果数据存储API使用反射,我认为它确实使用了反射,它无法访问未导出的结构字段,即不以大写字母开头的字段名。

将它们导出,然后它应该可以工作。

英文:

If the datastore API uses reflection, which I presume it does, it cannot access struct fields that aren't exported, i.e. field names that do not begin with a capital letter.

Export them and it should work.

huangapple
  • 本文由 发表于 2013年5月22日 12:31:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/16683495.html
匿名

发表评论

匿名网友

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

确定