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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大