liveBook · Manning

Conditional logic

As explained in Learn Go with Pocket-Sized Projects

A conditional action is a template control-flow action that selects output based on one or more conditions.

Conditional logic selects output or behavior when a condition is met. Use Go if statements for simple binary decisions; choose switch statements or maps for more complex cases. section 2.6 section 11.6

Go templates express conditional behavior with if, else if, and else actions enclosed by {{ and }}. The condition can use the current template value, called the dot, along with exposed fields. section 11.6 section 11.2

Choose template output

A template can branch on a condition: {{ if <cond> }}...{{ else if <cond> }}...{{ else }}...{{ end }}. This selects one output path while rendering the template. section 11.6

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines Go if statements and template if/else chains, without providing a worked conditional-template implementation.
Examples
Uses template actions such as {{ if <cond> }}...{{ else if <cond> }}...{{ else }}...{{ end }}.
What it assumes
Assumes familiarity with template data access through the current value, called the dot.
What you can do after
Apply it via Go if statements and template actions to select output for binary or chained conditions.
How it connects
Connects binary conditions with switch statements and maps, and template conditionals with actions, dot, and exposed fields.

Also explained in

Compare how 21 books explain conditional logic →

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.