Use the {{if}}
action and the eq
template function to compare the ENV
variable to certain values:
{{$ENV := "prod"}}
{{$folder := ""}}
{{if eq $ENV "prod"}}
{{$folder = "production"}}
{{else if eq $ENV "qa"}}
{{$folder = "qa"}}
{{else}}
{{$folder = "dev"}}
{{end}}
Note that this is not "conditional declaration". We declare $folder
"unconditionally" and used {{if}}
to change its value.
Here's a runnable demo to test the above template:
func main() {
t := template.Must(template.New("").Parse(src))
for _, env := range []string{"prod", "qa", "other"} {
if err := t.Execute(os.Stdout, env); err != nil {
panic(err)
}
}
}
const src = `{{$ENV := .}}
{{- $folder := "" -}}
{{- if eq $ENV "prod"}}
{{$folder = "production"}}
{{else if eq $ENV "qa"}}
{{- $folder = "qa" -}}
{{else}}
{{- $folder = "dev" -}}
{{end}}
$ENV = {{$ENV}}; $folder = {{$folder}}`
Output (try it on the Go Playground):
$ENV = prod; $folder = production
$ENV = qa; $folder = qa
$ENV = other; $folder = dev