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