Skip to content

Go Templates

Boilerplate uses Go’s built-in text/template engine. Every file in your template directory (except boilerplate.yml) is processed through this engine.

Variables defined in boilerplate.yml are accessed with dot notation:

Hello, {{ .ProjectName }}!
The port is {{ .Port }}.
{{ if .EnableFeature }}
Feature is enabled!
{{ else }}
Feature is disabled.
{{ end }}
{{ if eq .Environment "production" }}
Production mode
{{ else if eq .Environment "staging" }}
Staging mode
{{ else }}
Development mode
{{ end }}

Available comparison functions:

FunctionMeaningEquivalent
eqequal==
nenot equal!=
ltless than<
leless than or equal<=
gtgreater than>
gegreater than or equal>=

Go templates use function-call syntax rather than infix operators, so eq .Port 8080 means .Port == 8080. You can also pass multiple arguments: eq .Env "dev" "development" returns true if .Env equals either value.

{{ range .Tags }}
- {{ . }}
{{ end }}
{{ range $key, $value := .Config }}
{{ $key }}: {{ $value }}
{{ end }}

Chain functions with the pipe operator:

{{ .ProjectName | lower }}
{{ .ProjectName | upper | replace " " "_" }}

Assign values to local variables:

{{ $name := .ProjectName | lower }}
The slug is: {{ $name }}

Use - to trim whitespace around template actions:

{{- .ProjectName -}}

File and directory names can also use template syntax:

{{ .ProjectName | snakecase }}_service.go
com/{{ .PackageName }}/MyClass.java

By default, referencing an undefined variable causes an error. Control this with --missing-key-action:

ValueBehavior
error (default)Exit with an error
zeroRender zero value (empty string, 0, false)
invalidRender <no value>