我遇到了一个独特的问题。为了学习 golang,我创建了一个 Twitter 类的网站。它有推文,每条推文都可以有评论,每个评论都可以有子评论。
在 homepage.html 中显示 struct pd
Env.Tpl.ExecuteTemplate(w, "homePage.html", pd)
其中 pd 是页面数据(为了简单起见,我删除了额外的信息)
type PageData struct {
TweetView []tweets.TweetView
}
tweet.TweetView 在哪里
type TweetView struct {
Tweet
CV []comments.Comment
}
comments.Comment 在哪里
type Comment struct {
TweetID int64
ParentCommentID int64
CommentID int64
CreatedAt time.Time
Name string
UserID int64
CommentMsg string
}
这行得通。但是如果我用 comment.CommentView 更改 tweetView 中的 CV .. 模板将停止显示 TweetView。
comment.CommentView是
类型 CommentView 结构 { 评论 抄送[]评论
新的 TweetView 将被定义为
type TweetView struct {
Tweet
CV []comments.CommentView
}
在尝试进行数据存储查询以将推文对象提取到 Tweetview 中时出现此错误
err := datastore.Get(ctx, tweetKey, &tweetView[v])
datastore: flattening nested structs leads to a slice of slices: field "CV",
我觉得是golang的局限性。我该怎么办?
最佳答案
我能够解决问题。问题出在数据存储区。获取查询。
运行时报错
err := datastore.Get(ctx, tweetKey, &tweetView[v])
datastore: flattening nested structs leads to a slice of slices: field "CV",
所以我把查询改成了这样
var tweetTemp Tweet
datastore.Get(ctx, tweetKey, &tweetTemp)
tweetSlice[v].Tweet = tweetTemp
如果您发现此方法有问题,请告诉我
关于golang 不支持模板的结构 slice 深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40826521/