与GAE一起将一个对象保存到数据存储中。当它恢复时,该对象具有空成员 [英] Go with GAE saved an object to datastore. when getting it back, the object has empty members
问题描述
我创建了2个处理程序。一个用于将对象保存到数据存储区,另一个用于检索并输出到屏幕。问题是,当我从数据存储中检索到UserAccount对象时,对象中的每个值都没有了。
任何帮助都是值得赞赏的。
输出:
a / c计数: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)
retu
$ b fmt.Fprintf(w,val:%#v \ n,newAccount)
}
func get_info(w http.ResponseWriter,r * http.Request){
c:= appengine.NewContext(r)
$ bq:= 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;我< LEN(帐户); i ++ {
fmt.Fprintf(w,val:%#v \ n,accounts [i])
}
}
如果数据存储区API使用反射,我认为它会这样做,它无法访问未导出的结构字段,即不以大写字母开头的字段名称。
导出它们并且它应该可以工作。
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 \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])
}
}
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.
这篇关于与GAE一起将一个对象保存到数据存储中。当它恢复时,该对象具有空成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!