liveBook · Manning

Pipeline

As explained in Go in Practice, Second Edition

A template pipeline is a Go template construct that sequences commands and supports value processing and control logic.

A template pipeline chains commands with |, passing each command’s output into the next command as its last argument. This keeps presentation formatting in the template while application logic supplies the underlying data. section 9.1.2

Use pipelines to:

  • Format values, such as passing a time value through dateFormat with a layout string. section 9.1.2
  • Combine built-in operations such as urlquery and html when constructing links or displaying content. section 9.1.1
  • Iterate over slices with range and use nested conditional logic; inside range, . refers to the current item and $. accesses the outer scope. section 9.1.1

Formatting a date through a custom pipeline function

Register a Go function before parsing the template: funcMap := template.FuncMap{"dateFormat": dateFormat}; t := template.New("date").Funcs(funcMap); t = template.Must(t.Parse(tpl)). The template can then use {{.Date | dateFormat "Jan 2, 2006"}}. The function map exposes Go functions under template names, while the pipeline supplies .Date as the function’s final argument. section 9.1.2

{{"output" | printf "%q"}}

A minimal pipeline shows a literal flowing into printf and demonstrates the command-line-pipe analogy. section 9.1.2

How Go in Practice, Second Edition covers it

How deep it goes
Explains chaining, final-argument passing, custom-function registration, and range scope, but stops short of broader template internals.
Examples
Uses date formatting, quoted printf output, transformed links, and comment iteration with current and outer-scope values.
What it assumes
Assumes basic Go templates and supplied data, while demonstrating custom function maps and range scope explicitly.
What you can do after
Apply it via html/template to chain built-in commands and registered Go functions while formatting or controlling rendered output.
How it connects
Connects Go templates with UNIX pipes, template functions, output escaping, range iteration, and template scope.

Also explained in

Compare how 63 books explain pipeline →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.