0
我坚持一个独特的问题。为了学习golang,我创建了一个twitter类型的网站。它有推文,每条推文可以有评论,每条评论可以有评论。golang不支持结构片深度与模板
显示结构中的PD homepage.html
Env.Tpl.ExecuteTemplate(w, "homePage.html", pd)
其中PD是pagedata(I去除为了简化额外信息)
type PageData struct {
TweetView []tweets.TweetView
}
哪里tweet.TweetView是
type TweetView struct {
Tweet
CV []comments.Comment
}
其中评论是
type Comment struct {
TweetID int64
ParentCommentID int64
CommentID int64
CreatedAt time.Time
Name string
UserID int64
CommentMsg string
}
这是有效的。但如果我用tweetView更改简历,评论.CommentView ..模板停止显示TweetView。
comment.CommentView是
类型评论查看结构{ 注释 CC []注释 }
新TweetView将被定义为
type TweetView struct {
Tweet
CV []comments.CommentView
}
获取此错误,尝试时进行数据存储查询以将推文对象提取到Tweetview中
err := datastore.Get(ctx, tweetKey, &tweetView[v])
数据存储:扁平化嵌套结构导致片的切片:场 “CV”,
我认为这是golang的限制。我该怎么办?
你所要求的是明确你可以做的事情。你的模板中有一些东西:你应该发布一些简化版本。 – neclepsio
模板没有问题,因为即使我不使用模板中的简历它不显示任何内容。但是如果我从TweetView编辑出这个CV变量。它会再次运行。{{range .TweetSlice}} {{range .CV}} hello {{end}} {{end}} –
您确定您传递的数据是正确的吗? (顺便说一句,TweetSlice是什么?)。我认为你应该发布[最小,完整和可验证的例子](http://stackoverflow.com/help/mcve)。 – neclepsio