【发布时间】:2017-04-09 11:58:09
【问题描述】:
我将articles 的一部分发送到模板中。每个articlestruct 就像:
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}}
但我明白了:
模板文件错误:模板:articles_list.tmpl:14:33:正在执行 <.articles.content> 处的“内容”:无法评估类型中的字段内容 界面{}
如果我只是调用
{{index .articles 0}}
它显示了整个 article[0] 对象。
我该如何解决这个问题?
【问题讨论】:
-
为什么这个问题被否决了?
标签: go go-templates