我有两个结构如下,我需要使用模板包在模板上呈现数据.我收到这个错误
<.Email>: Email is not a field of struct type Notes.
问题似乎是只有范围结构的字段似乎在范围循环中可用,所以我想知道如何从范围结构外部导入字段(例如电子邮件字符串).
这种行为非常意外.
type notes struct{
Note string
sf string
}
type uis struct{
notes []Note
Email string
}
var ui uis
Run Code Online (Sandbox Code Playgroud)
HTML
{{range .notes}}
{{.Email}} {{.sf}}
{{end}}
Email {{.Email}}
Run Code Online (Sandbox Code Playgroud)
我检查过godocs但它们似乎没用.
Sim*_*ead 16
从文档:
执行开始时,$设置为传递给Execute的数据参数,即dot的起始值.
因此,您可以使用此:
{{range .notes}}
{{$.Email}} {{.sf}}
{{end}}
Email {{.Email}}
Run Code Online (Sandbox Code Playgroud)
(注意范围内的美元符号)
游乐场链接:http://play.golang.org/p/XiQFcGJEyR
旁注:下次尝试提供正确的代码和更好的解释.就目前而言,我想我已经回答了这个问题,但我无法确定.您的代码无法编译 - 例如,类型名称错误/与成员混合,您有未导出的字段,因此模板无法访问它们.