简体   繁体   English

删除文件开头的新行 - Go 模板

[英]Remove new line at the beginning of the file - Go template

I'm using fallowing loop to generate credentials file in Hashicorp Vault.我正在使用休闲循环在 Hashicorp Vault 中生成凭据文件。 All works well but I'm getting one new line at the beginning of the file.一切正常,但我在文件的开头得到了一个新行。 How can I remove it?我怎样才能删除它?

vault.hashicorp.com/agent-inject-template-credentials.txt: |
  {{- with secret (print "secret/data/test/config") }}{{- range $k, $v := .Data.data }}
  {{ $k }}: {{ $v }}
  {{- end }}{{- end }}

Input: map[test1:test1 test2:test2 test3:test3]输入: map[test1:test1 test2:test2 test3:test3]

Current output:当前 output:

// one empty line at the beginning
test1: test1
test2: test2
test3: test3

Your template contains a newline before rendering the elements, use the - sign to get rid of that:您的模板在渲染元素之前包含一个换行符,使用-符号来摆脱它:

{{- with secret (print "secret/data/test/config") }}{{- range $k, $v := .Data.data -}}
{{ $k }}: {{ $v }}
{{- end }}{{- end }}

Note the added - sign at the end of the first line.注意第一行末尾添加的-符号。

This of course will render each pair on the same line.这当然会将每一对渲染在同一行上。 Leave the newline at the end of rendering the elements by removing the - sign from the beginning of the last line:通过从最后一行的开头删除-符号,将换行符留在渲染元素的末尾:

{{- with secret (print "secret/data/test/config") }}{{- range $k, $v := .Data.data -}}
{{ $k }}: {{ $v }}
{{ end }}{{- end }}

Alternatively you can move the first added - sign to the beginning of the second line:或者,您可以将第一个添加的-符号移动到第二行的开头:

{{- with secret (print "secret/data/test/config") }}{{- range $k, $v := .Data.data }}
{{- $k }}: {{ $v }}
{{ end }}{{- end }}

These templates will output (no first empty line):这些模板将 output(没有第一个空行):

test1: test1
test2: test2
test3: test3

Try it on the Go Playground .Go Playground上试一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM