go的text/template包不支持直接使用==等运算符进行字段比较,需借助eq等内置函数实现条件判断,本文详解其用法、原理及最佳实践。
在 Go 的 text/template 包中,模板语言是有意简化且无状态的——它不支持原生的中缀比较操作符(如 ==、!=、> 等),这是出于安全性和设计哲学的考量:避免在模板层引入复杂逻辑,强制将业务判断留在 Go 代码中,模板仅负责呈现。
因

tpl := `{{if .Brand == "Coke"}} It's a coke{{else}} It's something else{{end}}`
// ❌ template: test:1: unexpected "=" in if正确方式是使用模板内置的比较函数,最常用的是 eq(equal):
package main
import (
"os"
"text/template"
)
type Something struct {
Brand string
}
func main() {
t := template.Must(template.New("brand").Parse(
`{{if eq .Brand "Coke"}}It's a coke{{else}}It's something else{{end}}`,
))
data := Something{Brand: "Coke"}
_ = t.Execute(os.Stdout, data) // 输出:It's a coke
}eq 函数接受两个及以上参数,依次比较是否全相等(类似 a == b && b == c),支持任意可比较类型(字符串、数字、布尔、nil 等)。除 eq 外,text/template 还提供以下常用比较函数:
| 函数 | 说明 | 示例 |
|---|---|---|
| eq a b | a == b | {{eq .Status "active"}} |
| ne a b | a != b | {{ne .Count 0}} |
| lt a b | a | {{lt .Age 18}} |
| le a b | a | {{le .Score 100}} |
| gt a b | a > b | {{gt .Price 50.0}} |
| ge a b | a >= b | {{ge .Length 10}} |
⚠️ 注意事项:
✅ 最佳实践建议:
综上,text/template 虽不支持内联运算符,但通过 eq、lt 等函数已能覆盖绝大多数字段评估场景——关键在于理解其“函数式”而非“表达式式”的设计范式。