Why is a Dash Used in Conditional Go Templates?
When writing Go templates, dashes (--) are commonly used in if statements. For example:
{{- if hasKey .Values.mymap "mykey" }} # do something conditional here... {{- end }}
The dash in this statement serves a crucial purpose: it removes any whitespace from the output on the side it appears on.
According to the Go documentation on Text and Spaces:
{{- if ...}}
This syntax removes all spaces that precede the if statement. Therefore, if the result of the if statement prints something, it will be directly after the last piece of text without any whitespace. This is useful when you want to ensure that the output is compact and visually appealing.
By using dashes, Go templates can maintain a consistent and clean appearance, even when dealing with conditional statements that may or may not produce output.
The above is the detailed content of Why Are Dashes Used in Conditional Go Templates?. For more information, please follow other related articles on the PHP Chinese website!