2016-11-27 30 views
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的限制。我該怎麼辦?

+0

你所要求的是明確你可以做的事情。你的模板中有一些東西:你應該發佈一些簡化版本。 – neclepsio

+0

模板沒有問題,因爲即使我不使用模板中的簡歷它不顯示任何內容。但是如果我從TweetView編輯出這個CV變量。它會再次運行。{{range .TweetSlice}} {{range .CV}} hello {{end}} {{end}} –

+0

您確定您傳遞的數據是正確的嗎? (順便說一句,TweetSlice是什麼?)。我認爲你應該發佈[最小,完整和可驗證的例子](http://stackoverflow.com/help/mcve)。 – neclepsio

回答

0

我能解決這個問題。問題出在數據存儲。獲取查詢。

這是給下面的錯誤,當我運行

err := datastore.Get(ctx, tweetKey, &tweetView[v]) 

數據存儲:扁平化嵌套結構導致片的切片:場 「CV」,

所以我改變這樣

var tweetTemp Tweet 
datastore.Get(ctx, tweetKey, &tweetTemp) 
tweetSlice[v].Tweet = tweetTemp 

查詢請讓我知道如果你看到這種方法的問題