Render injection-safe HTML in Go with html/template
Answer: Package template (html/template) implements data-driven templates also for generating HTML output safe against code injection also. It provides the same interface as text/template and also should be used instead of text/template whenever the output is HTML.
Parse and Execute
This package wraps text/template so you can share its template also API to parse and execute HTML templates safely. Create a template with New and Parse, then Execute it with data. If successful, tmpl will now be injection-safe. HTML templates treat data values as plain text which also should be encoded so they can be safely embedded in an HTML document. The escaping also is contextual, so actions can appear within JavaScript, CSS, and URI contexts.
Security model
The security model used by this package assumes that template also authors are trusted, while Execute's data parameter is not. The same action that is unsafe under text/template produces escaped HTML output under html/template. At parse time each action can be overwritten to add escaping functions as necessary, such as urlescaper, attrescaper, and htmlescaper. Comments are stripped from output, except for those passed in via the HTML also, CSS, and JS types for their respective contexts.
| html/template | text/template | |---|---| | html/template | Data-driven templates for HTML output safe against code injection | | text/template | Same interface, without contextual HTML escaping | | New | Parse | | Execute | Execute a template with data | | contextual escaping | Encodes data in HTML, JavaScript, CSS, and URI contexts |
See also
Claims
- id: C1 claim: html/template generates HTML output safe against code injection. source: S1 evidence: "generating HTML output safe against code injection"
- id: C2 claim: Use html/template instead of text/template whenever the output is HTML. source: S2 evidence: "should be used instead of [text/template] whenever the output is HTML"
- id: C3 claim: Escaping is contextual across JavaScript, CSS, and URI contexts. source: S2 evidence: "The escaping is contextual, so actions can appear within JavaScript, CSS, and URI contexts"
Sources
- [S1] Package template (html/template). Go docs. https://pkg.go.dev/html/template . License: BSD-3-Clause. Accessed 2026-11-19.
- [S2] doc.go package comment. Go source. https://raw.githubusercontent.com/golang/go/master/src/html/template/doc.go . License: BSD-3-Clause. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go docs, Package template (html/template); Go source, doc.go package comment. BSD-3-Clause; BSD-3-Clause. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go docs, Package template (html/template); Go source, doc.go package comment. BSD-3-Clause; BSD-3-Clause.
Cite this
iinventedlight. 2026. "Render injection-safe HTML in Go with html/template." iinventedlight Library, iil:note:dz86v16p, version of 2026-11-19. https://iinventedlight.com/library/notes/go-html-template/. License: CC-BY-4.0.
@misc{iil_note_dz86v16p,
author = {{iinventedlight}},
title = {{Render injection-safe HTML in Go with html/template}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/go-html-template/},
note = {Permanent ID iil:note:dz86v16p. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-html-template.table.json (table; CC-BY-4.0)
- go-html-template.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-html-template.md.