我有一个模板,我想使用text/template包来评估各个字段。我很难弄清楚评估应该如何工作,因为下面的代码似乎失败了。模板包是否足以处理这种评估?
type something struct{
Brand string
}
tpl := `{{if .Brand == "Coke"}} It's a coke{{else}} It's something else{{end}}`
答案 0 :(得分:1)
您可以调用名为eq
的模板包中的全局函数。不知道为什么它会这样,但这里是代码
type something struct{
Brand string
}
tpl := `{{if eq .Brand "Coke"}} It's a coke{{else}} It's something else{{end}}`