← All notes

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

Sources

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

Permanent ID: iil:note:dz86v16p

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.},
}

Markdown

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.

All artifact files (JSON index)

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.