【发布时间】:2021-02-18 19:55:25
【问题描述】:
根据html/template 文档中的示例,我不能说我完全理解为什么在我的实验中出现小于和大于不一致的转义:
https://golang.org/pkg/html/template/#hdr-Introduction
这是否需要报告错误?我推迟了,因为我对 Go 比较陌生。
$ go version
go version go1.16 linux/amd64
我在go1.15.8 看到了类似的行为。
package main
import (
htmltemplate "html/template"
"os"
texttemplate "text/template"
)
type MyVars struct {
Flavor string
}
func main() {
Vars := MyVars{
Flavor: "##### html #####",
}
htmlTmpl, _ := htmltemplate.ParseFiles("index.html")
htmlTmpl.Execute(os.Stdout, Vars)
Vars = MyVars{
Flavor: "##### text #####",
}
textTmpl, _ := texttemplate.ParseFiles("index.html")
textTmpl.Execute(os.Stdout, Vars)
}
$ cat index.html
{{ .Flavor }}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
< span >Hello< /span >
<span>Hello</span>
{{ "<" }}span{{ ">" }}Hello{{ "<" }}/span{{ ">" }}
$ ./experiment
##### html #####
<?xml version="1.0" encoding="UTF-8" standalone="no"?> # Why is only < escaped?
< span >Hello< /span > # Why is only < escaped?
<span>Hello</span> # Why is neither < nor > escaped?
<span>Hello</span>
##### text #####
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
< span >Hello< /span >
<span>Hello</span>
<span>Hello</span>
【问题讨论】:
标签: html go templates escaping auto