3 回答

TA貢獻1863條經驗 獲得超2個贊
intID 是Key的內部屬性,而不是struct,可以通過getter進行訪問:
id := key.IntID()
GetAll返回[]*Key,您沒有使用它:
_, err := q.GetAll(c, &blogposts)
解決此問題的一種方法是創建一個既包含帖子信息又包含關鍵信息的viewmodel結構(未經測試,但這是要點):
//... handler code ...
keys, err := q.GetAll(c, &blogposts)
if err != nil {
http.Error(w, "Problem fetching posts.", http.StatusInternalServerError)
return
}
models := make([]BlogPostVM, len(blogposts))
for i := 0; i < len(blogposts); i++ {
models[i].Id = keys[i].IntID()
models[i].Title = blogposts[i].Title
models[i].Content = blogposts[i].Content
}
//... render with mustache ...
}
type BlogPostVM struct {
Id int
Title string
Content string
}
- 3 回答
- 0 關注
- 211 瀏覽
添加回答
舉報