我想修剪Go模板中的空白。我该怎么办?
例子:
{{ $title = " My Title of the product " }}
// Print the trim string here
<h1>{{ $title }}</h1>
最佳答案
内置的任何东西都不会在模板内为您修剪字符串“管道”,但是,如果您使用 strings.TrimSpace
方法将其提供给模板,则可以在模板内使用 Funcs
函数。
var str = `{{ $title := " My Title of the product " }}
// Print the trim string here
<h1>{{ trim $title }}</h1>`
t := template.Must(template.New("t").Funcs(template.FuncMap{
"trim": strings.TrimSpace,
}).Parse(str))
https://play.golang.org/p/g0T7shJbDVw。
关于go - 如何在Go模板中修剪空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62531276/