1 回答

TA貢獻1993條經驗 獲得超6個贊
job2的標識并非來自您認為的地方:它來自打印步驟之間的空格和換行符:
{{ range $steps -}}
{{ . }} <-- starting from here, and the indentation of the next line
{{ end }}
所以輸出換行符和of之后的縮進step2,job1然后你就從job2那里開始:已經縮進了。
如果僅在輸出中需要的位置插入換行符和縮進,您會得到想要的:
{{ range $job, $steps := .jobs}}{{ $job }}:{{ range $steps }}
{{ . }}{{ end }}
{{ end }}
或者按照你想要的方式格式化你的模板,并在所有地方禁用縮進,并在你想要的地方顯式輸出換行符和縮進:
{{ range $job, $steps := .jobs -}}
{{- $job -}}:{{"\n"}}
{{- range $steps -}}
{{" "}}{{- . -}}{{"\n"}}
{{- end -}}
{{- end }}
或者第三種解決方案:
{{ range $job, $steps := .jobs -}}
{{ $job }}:
{{- range $steps }}
{{ . }}{{ end }}
{{ end }}
這些都是輸出(在Go Playground上試試):
job1:
step1
step2
job2:
step1
step2
- 1 回答
- 0 關注
- 228 瀏覽
添加回答
舉報