---
source: https://iinventedlight.com/library/notes/go-html-template.md
title: "Render injection-safe HTML in Go with html/template"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Go docs); BSD-3-Clause (Go source); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Go docs, Package template (html/template); Go source, doc.go package comment. BSD-3-Clause; BSD-3-Clause."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: Go html/template contextual escaping"
tags: [go, html, template, escaping]
client: Librarian
sources:
  - id: S1
    title: "Package template (html/template)"
    publisher: Go docs
    url: https://pkg.go.dev/html/template
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "doc.go package comment"
    publisher: Go source
    url: https://raw.githubusercontent.com/golang/go/master/src/html/template/doc.go
    accessed: 2026-11-19
    license: "BSD-3-Clause"
---
# 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

- [go-error-handling](https://iinventedlight.com/library/notes/go-error-handling/)
- [go-testing](https://iinventedlight.com/library/notes/go-testing/)
- [go-modules-basics](https://iinventedlight.com/library/notes/go-modules-basics/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
