'Go Template Extra line on the Range
I have Following go Template code
custom_listeners: {{ range $cl := $.Vars.CustomListeners }}
{{ $cl.ListenerType }}:
{{ range $k,$v := $cl.Values }}{{ $k }}: "{{ $v }}"
{{ end }}{{ end }}
Which creates the following YAML file . However the looping through the services add extra line between the service. I have tried different method to remove this line but it messes the yml formatting.
custom_listeners:
service1:
name: "service1"
port: "8091"
ssl_enabled: "false"
<-------------------------- Extra Line
service2:
name: "service2"
port: "8092"
ssl_enabled: "false"
<-------------------------- Extra Line
service3:
name: "service3"
port: "9093"
ssl_enabled: "false"
Just wondering what would be the best to way to get Desired result below :
custom_listeners:
service1:
name: "service1"
port: "8091"
ssl_enabled: "false"
service2:
name: "service2"
port: "8092"
ssl_enabled: "false"
service3:
name: "service3"
port: "7093"
ssl_enabled: "false"
Solution 1:[1]
https://pkg.go.dev/text/template#hdr-Text_and_spaces
... if an action's left delimiter (by default "{{") is followed immediately by a minus sign and white space, all trailing white space is trimmed from the immediately preceding text. Similarly, if the right delimiter ("}}") is preceded by white space and a minus sign, all leading white space is trimmed from the immediately following text. ...
custom_listeners:
{{- range $cl := $.Vars.CustomListeners }}
{{ $cl.ListenerType }}:
{{- range $k,$v := $cl.Values }}
{{ $k }}: "{{ $v }}"
{{- end }}
{{- end }}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
