英文:
How to create a data model using Go's dataStore?
问题
我已经根据我在MySQL中创建表的方式创建了一系列的结构体:
type User struct {
UserID int
Email string
Password string
DateCreated time.Time
}
type Device struct {
DeviceID int
Udid string
DateCreated time.Time
DateUpdated time.Time
IntLoginTotal int
}
type DeviceInfo struct {
DeviceID int
DeviceName string
Model string
LocalizedModel string
SystemName string
SystemVersion string
Locale string
Language string
DateCreated time.Time
}
然而,我有一种印象,即我将无法进行这样的请求,而是需要创建一个单一的结构体,该结构体可以包含多个设备的数组(每个设备包含多个设备信息记录的数组)。
如何最好地实现这个目标?
英文:
I've created a series of structs based on the way I would create tables in mySQL:
type User struct {
UserID int
Email string
Password string
DateCreated time.Time
}
type Device struct {
DeviceID int
Udid string
DateCreated time.Time
DateUpdated time.Time
IntLoginTotal int
}
type DeviceInfo struct {
DeviceID int
DeviceName string
Model string
LocalizedModel string
SystemName string
SystemVersion string
Locale string
Language string
DateCreated time.Time
}
However, I have the impression that I will not be able to make requests like this, and instead I need to create a single struct that can contain an array of multiple devices (each containing an array of multiple device info records).
What is the best way to go about doing this?
答案1
得分: 1
在这种情况下,只需将"Kind"设置为结构体的名称。
根据文档:
func NewKey(c appengine.Context, kind, stringID string, intID int64, parent *Key) *Key
NewKey创建一个新的键。kind不能为空。stringID和intID中的一个或两个必须为零。如果两者都为零,则返回的键是不完整的。parent必须是一个完整的键或nil。
例如,保存一个用户:
c := appengine.NewContext(r)
u := &User{UserID: userid, Email: email, Password: password, DateCreated: datecreated}
k := datastore.NewKey(c, "User", u.UserID, 0, nil)
e := p
_, err := datastore.Put(c, k, e)
按照相同的逻辑保存不同的结构体类型。
加载用户:
c := appengine.NewContext(r)
k := datastore.NewKey(c, "User", userid, 0, nil)
e := new(User)
_, err := datastore.Get(c, k, e)
英文:
In this case, just set the "Kind" as the name of the struct.
From the docs:
func NewKey(c appengine.Context, kind, stringID string, intID int64, parent *Key) *Key
> NewKey creates a new key. kind cannot be empty. Either one or both of
> stringID and intID must be zero. If both are zero, the key returned is
> incomplete. parent must either be a complete key or nil.
For example to save a User.
c := appengine.NewContext(r)
u := &User{UserID: userid, Email: email, Password: password, DateCreated: datecreated}
k := datastore.NewKey(c, "User", u.UserID, 0, nil)
e := p
_, err := datastore.Put(c, k, e)
Follow the same logic to save a different struct type.
To load a user:
c := appengine.NewContext(r)
k := datastore.NewKey(c, "User", userid, 0, nil)
e := new(User)
_, err := datastore.Get(c, k, e)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论