articles
のスライスをテンプレートに送信します。各article
構造体は次のようである:テンプレートのインデックスでフィールドを取得する方法は?
type Article struct {
ID uint32 `db:"id" bson:"id,omitempty"`
Content string `db:"content" bson:"content"`
Author string `db:"author" bson:"author"`
...
}
私は{{range $n := articles}}
でarticles
スライスをループすることができ、各{{$n.Content}}
を得るが、私がしたいことは見出しに使用する(レンジループの外側)のみ最初のものを持っていることです。 私が試したことはある:
{{index .articles.Content 0}}
しかし、私は得る:
Template File Error: template: articles_list.tmpl:14:33: executing "content" at <.articles.Content>: can't evaluate field Content in type interface {}
私はちょうどそれが全体の記事[0]オブジェクトを示し
{{index .articles 0}}
起動した場合。
どうすればこの問題を解決できますか?
うん、それは問題を解決した。私はそれほど冗長な方法はないと思ったが... – Karlom