google-cloud-datastore - Go with GAE 将对象保存到数据存储区。取回时,该对象的成员为空

标签 google-cloud-datastore go

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

如有任何帮助,我们将不胜感激。

输出:

账户数: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])
    }
}

最佳答案

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

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

关于google-cloud-datastore - Go with GAE 将对象保存到数据存储区。取回时,该对象的成员为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17133940/

相关文章:

python - GAE 中应该使用什么来存储数据?

java - KeyFactory.createKey() 这是在数据存储中存储数据的正确方法吗

node.js - Google App Engine Nodejs 错误 409

go - ffmpeg 获取机器可读的输出

go - 我正在尝试将 Cobra 集成到我的程序中

python - 如何获取尚未绑定(bind)的手动分配的父 ID?

java - 是否可以将 List<Key> 作为实体的属性存储在 Google App Engine (GAE) 中?

postgresql - postgres,go-gorm-无法预加载数据

使用 goroutine 去 WaitGroup

go - 包命名 : error exporting variable