3 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
我認(rèn)為您不需要地圖。據(jù)我了解,在GetAll之后,您有兩個(gè)平行的片,鍵和a。(我不知道GAE,但是_引起了我的注意。您應(yīng)該檢查一下嗎?)模板可以處理并行數(shù)組。在文檔中查看range操作如何返回兩個(gè)結(jié)果,即value和and index。您應(yīng)該能夠跨一個(gè)切片,并使用索引從另一個(gè)切片中獲取相應(yīng)的值。這將是一個(gè)更復(fù)雜的模板,但應(yīng)該讓您避開(kāi)地圖。
編輯:對(duì)不起,我以為我知道該怎么做,但是在嘗試編寫(xiě)示例時(shí)失敗了。如果有人知道如何做,我將其留在這里,否則,請(qǐng)投票...

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個(gè)贊
也許您可以將嵌入到Key中Article。您仍然必須遍歷文章和鍵,但至少不必創(chuàng)建單獨(dú)的地圖。
type Article struct {
// Add a key to the struct, but prevent it from being saved to the datastore.
Key datastore.Key `datastore:"-"`
}
// Query
q := datastore.NewQuery("Article").Limit(10)
// Define array where the entities will be retreived
var a[] Article
// Retreive entities
key, _ := q.GetAll(c, &a)
// Loop through the articles adding the key as you go.
for i := range a {
a[i].Key = key[i]
}
template.Execute(w, map[string]interface{} { "Articles" : a})
然后在您的模板中,您將調(diào)用 article.Key.Encode
- 3 回答
- 0 關(guān)注
- 246 瀏覽
添加回答
舉報(bào)