Go模板中Range语句的额外换行问题

huangapple go评论83阅读模式
英文:

Go Template Extra line on the Range

问题

我有以下的Go模板代码:

    custom_listeners:  {{ range $cl := $.Vars.CustomListeners }}
      {{ $cl.ListenerType }}:
        {{ range $k,$v := $cl.Values }}{{ $k }}: "{{ $v }}"
        {{ end }}{{ end }}

它创建了以下的YAML文件。然而,循环遍历服务时会在服务之间添加额外的空行。我尝试了不同的方法来删除这个空行,但会破坏YAML的格式。

    custom_listeners:
      service1:
        name: "service1"
        port: "8091"
        ssl_enabled: "false"
                            <-------------------------- 额外的空行
      service2:
        name: "service2"
        port: "8092"
        ssl_enabled: "false"
                             <-------------------------- 额外的空行
      service3:
        name: "service3"
        port: "9093"
        ssl_enabled: "false"

我想知道如何以最佳方式获得以下期望的结果:

    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"
英文:

I have Following go Template code

    custom_listeners:  {{ range $cl := $.Vars.CustomListeners }}
      {{ $cl.ListenerType }}:
        {{ range $k,$v := $cl.Values }}{{ $k }}: &quot;{{ $v }}&quot;
        {{ 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: &quot;service1&quot;
        port: &quot;8091&quot;
        ssl_enabled: &quot;false&quot;
                            &lt;-------------------------- Extra Line
      service2:
        name: &quot;service2&quot;
        port: &quot;8092&quot;
        ssl_enabled: &quot;false&quot;
                             &lt;-------------------------- Extra Line
      service3:
        name: &quot;service3&quot;
        port: &quot;9093&quot;
        ssl_enabled: &quot;false&quot;

Just wondering what would be the best to way to get Desired result below :

    custom_listeners:
      service1:
        name: &quot;service1&quot;
        port: &quot;8091&quot;
        ssl_enabled: &quot;false&quot;
      service2:
        name: &quot;service2&quot;
        port: &quot;8092&quot;
        ssl_enabled: &quot;false&quot;
      service3:
        name: &quot;service3&quot;
        port: &quot;7093&quot;
        ssl_enabled: &quot;false&quot;

答案1

得分: 2

以下是翻译好的内容:

https://pkg.go.dev/text/template#hdr-Text_and_spaces

> ... 如果一个动作的左定界符(默认为“{{”)紧跟着一个减号和空白字符,则所有尾随的空白字符将从前面的文本中删除。类似地,如果右定界符(“}}”)之前有空白字符和减号,则所有前导的空白字符将从后面的文本中删除。...

custom_listeners:
{{- range $cl := $.Vars.CustomListeners }}
	{{ $cl.ListenerType }}:
		{{- range $k,$v := $cl.Values }}
		{{ $k }}: &quot;{{ $v }}&quot;
		{{- end }}
{{- end }}

https://go.dev/play/p/aZ7tNqV2Phq

英文:

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 }}: &quot;{{ $v }}&quot;
		{{- end }}
{{- end }}

https://go.dev/play/p/aZ7tNqV2Phq

huangapple
  • 本文由 发表于 2022年1月25日 00:18:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/70836995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定