Golang模板范围换行符删除 [英] Golang template range newline removal
本文介绍了Golang模板范围换行符删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图弄清楚如何删除{{range}}和{{end}}放在模板中的新行.我得到以下输出,其中没有任何-"标签:
I'm trying to figure out how I can remove the new lines in my template that are put there by the {{range}} and {{end}}. I get the following output without any of the "-" tags:
type {{makeGoTableName .TableName}} struct {
{{range $key, $value := .TableData}}
{{makeGoColName $value.ColName}} {{$value.ColType}} `db:"{{makeDBColName $value.ColName}}",json:"{{$value.ColName}}"`
{{end}}
}
结果:
type Dogs struct {
ID int64 `db:"id",json:"id"`
DogNumber int64 `db:"dog_number",json:"dog_number"`
}
如果我像这样添加-标记,则可以使其接近理想值,但它破坏了最后一个右括号的缩进:
If I add the - tags like so, I can get it close to desirable but it breaks the indentation of the final closing brace:
type {{makeGoTableName .TableName}} struct {
{{range $key, $value := .TableData -}}
{{makeGoColName $value.ColName}} {{$value.ColType}} `db:"{{makeDBColName $value.ColName}}",json:"{{$value.ColName}}"`
{{end -}}
}
结果:
type Dogs struct {
ID int64 `db:"id",json:"id"`
DogNumber int64 `db:"dog_number",json:"dog_number"`
}
有什么想法吗?
推荐答案
主要是在使用斜杠时尝试
Its mostly on playing with the trailing slash, try
package main
import (
"os"
"text/template"
)
type myGreetings struct {
Greet []string
}
func main() {
const txt = `
{
{{- range $index, $word := .Greet}}
Hello {{$word -}}!!!
{{- end}}
}
`
greetText := myGreetings{
Greet: []string{"World", "Universe", "Gophers"},
}
t := template.Must(template.New("Text").Parse(string(txt)))
t.Execute(os.Stdout, greetText)
}
https://play.golang.org/p/eGm3d3IJPp
输出:
{
Hello World!!!
Hello Universe!!!
Hello Gophers!!!
}
这篇关于Golang模板范围换行符删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文