html/template 默认对变量输出做HTML转义防XSS,text/template不转义;html/template校验标签闭合,后者不校验;渲染HTML必须用前者,并设Content-Type。
html/template 和 text/template 有什么区别?直接用 html/template,别选 text/template 渲染 HTML。前者默认对变量输出做 HTML 转义(比如把 变成 zuojiankuohaophpcn),防止 XSS;后者不转义,容易出安全问题。
除非你明确要输出纯文本(如生成配置文件、邮件正文),否则一律用 html/template。
html/template 的 {{.Name}} 会自动转义 、>、&、"、'
safeHTML 字段),必须用 {{.Content | safeHTML}},且该字段类型得是 template.HTML
html/template 会校验标签闭合,text/template 不校验 —— 这意味着用错包可能让页面渲染出错但无提示典型流程:解析文件 → 构建数据 → 执行写入 http.ResponseWriter 或 bytes.Buffer。
注意路径是相对于 os.Getwd()(运行目录),不是源码目录;推荐用 embed(Go 1.16+)避免部署时漏模板文件。
package main
import (
"html/template"
"net/http"
"embed"
)
//go:embed templates/*.html
var templatesFS embed.FS
func handler(w http.ResponseWriter, r *http.Request) {
t := template.Must(template.ParseFS(templatesFS, "templates/*.html"))
data := struct{ Title string }{Title: "首页"}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
t.Execute(w, data)
}
template.ParseFiles("index.html") 简单但易因路径出错;ParseFS 更健壮template.Must() 在解析失败时 panic,适合启动期加载;线上服务建议自己捕获 error
Content-Type,否则浏览器可能误判编码,中文变乱码不能直接写 {{.RawHTML}},那会被转义成字符串字面量。必须显式标记为“可信”:
type PageData struct {
Title string
Content template.HTML // 类型必须是 template.HTML
}
func handler(w http.ResponseWriter, r *http.Request) {
data := PageData{
Title: "公告",
Content: temp
late.HTML(`欢迎
加粗`),
}
t := template.Must(template.New("").Parse(`{{.Title}}
{{.Content}}`))
t.Execute(w, data)
}
template.HTML,不是 string
{{.Content | printf "%s"}} 之类绕过 —— 不生效,且破坏安全模型template.HTML!应先用 bluemonday 等库净化模板执行失败通常静默返回空内容或 500,很难定位。关键错误类型:
template: xxx:xxx: unexpected EOF:模板文件末尾缺 {{end}} 或括号不匹配template: xxx:xxx: nil pointer evaluating interface {}.Field:传了 nil 结构体或字段未导出(首字母小写){{.Name}} 原样字符串:没调 Execute,或 Execute 返回 error 但被忽略Content-Type: text/html; charset=utf-8,或模板文件本身不是 UTF-8 编码调试建议:先用 bytes.Buffer 替代 http.ResponseWriter,打印出错时的完整 error 和输出内容。