eq 函数允许比较两个字符串是否相等{{if eq .Name "MyName"}}有没有办法测试一个字符串是否以(或包含)另一个字符串结尾?
1 回答
慕无忌1623718
TA贡献1744条经验 获得超4个赞
funcs := map[string]any{
"contains": strings.Contains,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix}
tmpl := `{{if hasSuffix . ".txt"}}yes!{{end}}`
t := template.Must(template.New("").Funcs(funcs).Parse(tmpl))
t.Execute(os.Stdout, "example.txt") // writes yes! to standard out
一些使用 Go 模板作为特性的应用程序(例如 Hugo 和 Helm)默认提供这些功能。
(h/t 到 mkopriva)。
- 1 回答
- 0 关注
- 199 浏览
添加回答
举报
0/150
提交
取消
