douzhengyi5022 2013-05-22 04:31
浏览 33
已采纳

与GAE一起将对象保存到数据存储区。 取回时,该对象具有空成员

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:"", 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 
", 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 
", len(accounts))

    for i := 0; i < len(accounts); i++ {
        fmt.Fprintf(w, "val: %#v 
", accounts[i])
    }
}
  • 写回答

1条回答 默认 最新

  • douqufan9148 2013-05-22 05:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿