go - How to escape special characters (other than <>&'") in golang templates? -
go templates auto-escape 5 characters <>&' , " (see html.escapestring)
so code like
check := func(err error) { if err != nil { log.fatal(err) } } t, err := template.new("foo").parse(`{{define "t"}}special chars: {{.}}{{end}}`) check(err) err = t.executetemplate(os.stdout, "t", "<>&' äåüöß") check(err)
returns (as can check here: go playground)
special chars: <>&' äåüöß
so how use golang templates render text may contain characters äåüöß?
it's not escape these myself, because e.g. "ä" escapes "ä" means efter escape manually, golang escapes "&" character part of second time , "&auml;" (but html-file should contain single escaped "ä" browser displays "ä").
these characters fine as-is in modern browsers, you'll need specify character encoding in webpage.
add line
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
in document's head.
for more on character encodings in html, see w3c documentation.
Comments
Post a Comment