Escape Curly Brackets in Templates
How Do I Escape Curly Brackets in Golangs Template System? Assume I Want to Print {{Hello World}}: Var Buf Bytes. Buffer // Tpl: = Template. Must(Template...
How do I escape curly brackets in golangs template system?
Assume I want to print {{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")
}
1 Answer
You can use a raw string constant.
tpl := template.Must(template.New("").Parse("{{`{{Hello World}}`}}"))