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: &lt;&gt;&amp;&#39; äåüöß 

so how use golang templates render text may contain characters äåüöß?

it's not escape these myself, because e.g. "ä" escapes "&auml;" means efter escape manually, golang escapes "&" character part of second time , "&amp;auml;" (but html-file should contain single escaped "&auml;" 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

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -