go - 在模板中转义大括号
问题描述
如何在 golangs 模板系统中转义大括号?
假设我要打印{{Hello World}}
:
var buf bytes.Buffer
// tpl := template.Must(template.New("").Parse(`{{ print "{{Hello World}}"}}`)) // this is working
tpl := template.Must(template.New("").Parse(`\{\{Hello World\}\}`)) // this is not
if err := tpl.Execute(&buf, nil); err != nil {
panic(err)
}
if buf.String() != "{{Hello World}}" {
panic("Unexpected")
}
解决方案
您可以使用原始字符串常量。
tpl := template.Must(template.New("").Parse("{{`{{Hello World}}`}}"))
推荐阅读
- ruby-on-rails - Omniauth-Saml with Devise - 映射和使用属性
- javascript - 如何提高 PixiJS 画布的质量?
- python - 数据帧中系列的多级过滤和频率计数
- java - 为什么 Glassfish 5.1 容器不调用单例中注入的 CDI 拦截器
- swift - 如何修复错误:线程 1:致命错误:索引超出范围”?
- laravel - Laravel Jensseger Mongodb 属于ToMany 附加字段
- batch-file - 批处理中的 findstr 语法
- grafana - Grafana URL 警报
- php - 使用 move_uploaded_file() 时权限被拒绝的问题
- entity-framework-core - 扩展方法不应用 LINQ where 子句