猿问

Go 中的切片指针与 GAE Datastore 是否有良好的“关系”?

根据App Engine 文档中的祖先查询,我可以执行以下操作:


type Team struct {

    Name string

}


type Player struct {

    Name string

}


// Save data first just for the test case

teamA := datastore.NewIncompleteKey(c, "Team", nil)

teamA, _ = datastore.Put(c, teamA, Team{"Team A"})

playerA := datastore.NewIncompleteKey(c, "Player", teamA)

playerA, _ = datastore.Put(c, playerA, Player{"Player A"})

playerB := datastore.NewIncompleteKey(c, "Player", teamA)

playerB , _ = datastore.Put(c, playerB, Player{"Player B"})


// query data

q := datastore.NewQuery("Team").Filter("Name=", "Team A").Limit(1).KeysOnly()

teams, _ := q.GetAll(c, nil)

q = datastore.NewQuery("Player").Ancestor(teams[0])

var players []Player

q.GetAll(c, &players)

但是......如果我想让 Team 包含一个指向玩家切片的指针,那么我会将它保存为 nil,当我查询它时,我会分配它,有点像这样:


type Team struct {

    Name string

    Players *[]Player `datastore:-`

}


type Player {

    Name string

}


// Save data first just for the test case

teamA := datastore.NewIncompleteKey(c, "Team", nil)

teamA, _ = datastore.Put(c, teamA, Team{"Team A", nil})


/* Saving player data goes here */


// query data

q := datastore.NewQuery("Team").Filter("Name=", "Team A").Limit(1)

var teams []Team

teamKeys, _ := q.GetAll(c, teams)

q = datastore.NewQuery("Player").Ancestor(teamKeys[0])

q.GetAll(c, teams[0].Players)

这是模拟关系的好方法吗?

对于我的应用程序,树状结构将是完美适合我手指的戒指。

或者……你还有什么建议吗?


猛跑小猪
浏览 142回答 1
1回答
随时随地看视频慕课网APP

相关分类

Go
我要回答