如何使用文本/模板包评估字段?走

时间:2015-03-08 18:30:53

标签: templates go

我有一个模板,我想使用text/template包来评估各个字段。我很难弄清楚评估应该如何工作,因为下面的代码似乎失败了。模板包是否足以处理这种评估?

type something struct{
    Brand   string
}
tpl := `{{if .Brand == "Coke"}} It's a coke{{else}} It's something else{{end}}`

1 个答案:

答案 0 :(得分:1)

您可以调用名为eq的模板包中的全局函数。不知道为什么它会这样,但这里是代码

type something struct{
    Brand   string
}
tpl := `{{if eq .Brand "Coke"}} It's a coke{{else}} It's something else{{end}}`