Interview Prep/Helm/Templating: Go Templates & Sprig

How do you declare and reassign variables in templates?

IntermediateConceptTemplating: Go Templates & Sprig
Answer

:= declares, = reassigns (Go 1.11+ templates):

{{- $fullName := include "shop.fullname" . -}}
{{- $port := 3000 -}}
{{- if .Values.api.port }}{{ $port = .Values.api.port }}{{ end -}}

Variables are scoped to the block where they are declared (if, range, with, define). A variable declared inside an if is not visible after its end, so declare first, then reassign inside the block.

All Helm questions Roadmap: Helm

More on Templating: Go Templates & Sprig