doucanshou6998 2016-06-14 14:44
浏览 1257
已采纳

如何在golang模板中转义特殊字符(<>&'“除外)?

Go templates auto-escape but only the five characters <>&' and " (see html.EscapeString)

so this 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 you can check here: Go Playground)

special chars: &lt;&gt;&amp;&#39; äåüöß

So how do i use golang templates to render text that may contain characters like äåüöß?

It's not like i could escape these myself, because e.g. "ä" escapes to "&auml;" which means that efter i escape manually, golang just escapes the "&" character part of that a second time and i get "&amp;auml;" (but the html-file should contain a single escaped "&auml;" so that the browser displays an "ä").

  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 磁盘异常导致工业相机GigE传图卡顿
      • ¥20 python中使用chatgpt为什么一直连接失败
      • ¥50 使用grpc遇到的问题
      • ¥100 运算速度优化问题,控制在四秒以内
      • ¥15 如何用matlab画出这样的图
      • ¥15 线性回归问题进行特征缩放后,为什么求得的参数不对了
      • ¥15 python爬取网页信息(主要为期刊影响因子、官网等),已有ISSN进行检索,但爬取过程找不到class该怎么办?
      • ¥15 找出5阶幻方的所有解的个数:275305224
      • ¥20 wireshark无网络波动
      • ¥15 C#ArcEngine二次开发引入类似Arcmap内属性表内容的操作