Why Are Dashes Used in Conditional Go Templates?

Linda Hamilton
Release: 2024-11-10 02:58:02
Original
448 people have browsed it

Why Are Dashes Used in Conditional Go Templates?

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 }}
Copy after login

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 ...}}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template