why dash is used in condition Go templates

1/6/2020

I've seen many examples of dash being use for if statements ("{{- if.."), e.g:

{{- if hasKey .Values.mymap "mykey" }}
    # do something conditional here...
{{- end }}

what is the purpose of the dash in that statement?

-- amit
go
go-templates
kubernetes-helm

1 Answer

1/6/2020

Dash removes the spaces from the output on the side it appears in the template:

https://golang.org/pkg/text/template/#hdr-Text_and_spaces

{{- if ...}}

The above will remove all spaces that appear before the if statement, so if the result of if prints something, it'll be right after the last piece of text without any spaces.

-- Burak Serdar
Source: StackOverflow