liveBook · Manning

Whitespace trimming

As explained in Learn Go with Pocket-Sized Projects

Whitespace trimming is a template syntax feature that removes whitespace adjacent to an action delimiter.

Whitespace trimming removes whitespace adjacent to a Go template action. Attach a minus sign to the curly bracket and separate it from the action body: {{- .}} or {{ . -}}. section 11.1.2

Use trimming when template output must meet surrounding text exactly:

  • Write {{ . }}% or {{.}}% to render a percentage without an unwanted space before the percent sign. section 11.1.2
  • Remember that trimming removes all adjacent newlines, tabs, nonbreaking spaces, and regular spaces, including intentionally written whitespace. section 11.1.2
  • Do not write {{-.}}; use {{- .}} so the minus sign touches the delimiter but remains separated from the action body. section 11.1.2

Equivalent rendered output

The pairs {{ . -}} % and {{.}}%, ${{ . }} and $ {{- .}}, and [ {{- . -}} ] and [{{.}}] produce the same outputs. section 11.1.2

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines delimiter placement, affected whitespace characters, and output equivalences, but does not detail parser internals.
Examples
Demonstrates percentage formatting, bracketed values, dollar signs, spaces, tabs, and newlines.
What it assumes
Assumes familiarity with template actions and curly-brace delimiters while clarifying trimming syntax from examples.
What you can do after
Apply it via Go template delimiters to control whitespace around rendered values.
How it connects
Connects Go templates with template actions, delimiters, and rendered HTML output.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.