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.
Accessing Variables
Section titled “Accessing Variables”Variables defined in boilerplate.yml are accessed with dot notation:
Hello, {{ .ProjectName }}!The port is {{ .Port }}.Conditionals
Section titled “Conditionals”{{ if .EnableFeature }}Feature is enabled!{{ else }}Feature is disabled.{{ end }}Comparison operators
Section titled “Comparison operators”{{ if eq .Environment "production" }} Production mode{{ else if eq .Environment "staging" }} Staging mode{{ else }} Development mode{{ end }}Available comparison functions:
| Function | Meaning | Equivalent |
|---|---|---|
eq | equal | == |
ne | not equal | != |
lt | less than | < |
le | less than or equal | <= |
gt | greater than | > |
ge | greater 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.
Iterating over lists
Section titled “Iterating over lists”{{ range .Tags }}- {{ . }}{{ end }}Iterating over maps
Section titled “Iterating over maps”{{ range $key, $value := .Config }}{{ $key }}: {{ $value }}{{ end }}Pipelines
Section titled “Pipelines”Chain functions with the pipe operator:
{{ .ProjectName | lower }}{{ .ProjectName | upper | replace " " "_" }}Variables Inside Templates
Section titled “Variables Inside Templates”Assign values to local variables:
{{ $name := .ProjectName | lower }}The slug is: {{ $name }}Whitespace Control
Section titled “Whitespace Control”Use - to trim whitespace around template actions:
{{- .ProjectName -}}Dynamic File Names
Section titled “Dynamic File Names”File and directory names can also use template syntax:
{{ .ProjectName | snakecase }}_service.gocom/{{ .PackageName }}/MyClass.javaMissing Keys
Section titled “Missing Keys”By default, referencing an undefined variable causes an error. Control this with --missing-key-action:
| Value | Behavior |
|---|---|
error (default) | Exit with an error |
zero | Render zero value (empty string, 0, false) |
invalid | Render <no value> |