我使用confd通过模板填充文件。
在这个文件中,我希望有一个移位和插入的元素列表。 此切片包含类似
的字符串0=container-1
1=container-2
2=container-3
3=container-4
(实际上,它是使用split
confd函数分割的字符串)。
我希望,在每个容器上,能够过滤容器名称并将列表移动到我的容器首先出现之后。
作为一个例子,在container-2
我希望得到结果
2=container-3
3=container-4
0=container-1
如何在confd go模板中执行此操作?我想我知道如何去做(但我在那种特定的语言中并不是那么好),但我不知道如何仅使用模板来做到这一点...
答案 0 :(得分:1)
如果您无法操作模板外部的切片/字符串,并且无法向模板添加自定义函数,则您将在模板内执行此操作。它更冗长,但可行。
一种方法是将两个循环嵌套在父循环中。父级将查找您想要忽略的容器,此时它将生成两个子循环,其中$i
保存要被忽略的索引。然后,第一个子循环可以列出索引大于$i
的容器,第二个子循环将列出索引小于$i
的容器。
{{range $i, $c := $cons}}
{{/* find item to be skipped */}}
{{if (eq $c $.Skip)}}
{{range $j, $c := $cons}}
{{/* list items that come after the one to be skipped */}}
{{if (gt $j $i)}}
{{$c}}
{{end}}
{{end}}
{{range $j, $c := $cons}}
{{/* list items that come before the one to be skipped */}}
{{if (lt $j $i)}}
{{$c}}
{{end}}
{{end}}
{{end}}
{{end}}