英文:
how to put struct data in datastore in go-lang
问题
如何在Go语言中将结构数据存储到数据存储中:
//创建结构体
type UserLogin struct{
userName string
passWord string
}
//赋值
p1 := UserLogin{"poonam", "mumbai123"}
p2 := UserLogin{passWord:"mumbai321", userName:"abcd"}
现在如何将上述p1、p2存储/放入/保存到数据存储中?
英文:
how to put struct data in datastore in go-lang:
//Struct is created.
type UserLogin struct{
userName string
passWord string
}
//values assigned
p1 := UserLogin{"poonam", "mumbai123"}
p2 := UserLogin{passWord:"mumbai321", userName:"abcd"}
Now how to store/put/save and get above p1, p2 into datastore?
答案1
得分: 2
type UserLogin struct{
UserName string
PassWord string
}
needs to be public fields
storing:
c := appengine.NewContext(r)
p1 := UserLogin{"poonam", "mumbai123"}
key := datastore.NewKey(c, "UserLogin",p1.UserName, 0, nil)
datastore.Put(c, key,&p1)
reading:
c := appengine.NewContext(r)
key := datastore.NewKey(c, "UserLogin", "", userName, nil)
var ul UserLogin
err := datastore.Get(c, key, &ul)
where r is the *http.Request
also you need todo something with error,
英文:
change struct to:
type UserLogin struct{
UserName string
PassWord string
}
needs to be public fields
storing:
c := appengine.NewContext(r)
p1 := UserLogin{"poonam", "mumbai123"}
key := datastore.NewKey(c, "UserLogin",p1.UserName, 0, nil)
datastore.Put(c, key,&p1)
reading:
c := appengine.NewContext(r)
key := datastore.NewKey(c, "UserLogin", "", userName, nil)
var ul UserLogin
err := datastore.Get(c, key, &ul)
where r is the *http.Request
also you need todo something with error,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论