Home > Backend Development > Golang > Why Use a Dash in Go Template Conditionals?

Why Use a Dash in Go Template Conditionals?

Patricia Arquette
Release: 2024-11-15 07:59:02
Original
1041 people have browsed it

Why Use a Dash in Go Template Conditionals?

Dash Utilization in Go Templates' Conditionals

In Go templates, the {{- if ...}} syntax is often encountered to execute conditional statements. The dash - before the if keyword serves a specific purpose.

Purpose of the Dash

The primary function of the dash in {{- if ...}} templates is to eliminate spaces from the output on the side of the template where it appears.

Output Modification

When the dash is placed before the if condition, as shown in the example:

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

It prevents any whitespace that precedes the conditional statement from being printed along with the output of the statement. This ensures that if the condition evaluates to true, its output will be printed immediately after the last piece of text, without any intervening whitespace.

This is particularly useful in situations where multiple statements are executed sequentially and you desire a compact output without any line breaks or spaces between them.

The above is the detailed content of Why Use a Dash in Go Template Conditionals?. 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