如何在Go语言中将结构数据存储到数据存储中

huangapple go评论98阅读模式
英文:

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,

huangapple
  • 本文由 发表于 2013年6月17日 15:05:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/17142036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定