String Golang模板:使用管道到大写字符串
我想使用String Golang模板:使用管道到大写字符串,string,templates,go,uppercase,String,Templates,Go,Uppercase,我想使用string.ToUpper将golang模板中的字符串大写,如: {{ .Name | strings.ToUpper }} 但这不起作用,因为strings不是我的数据的属性 我无法导入字符串包,因为警告我未使用该包 下面是脚本: 只需使用类似的()将ToUpper函数注入到模板中即可 import ( "bytes" "fmt" "strings" "text/template" ) type TemplateData struct {
string.ToUpper
将golang模板中的字符串大写,如:
{{ .Name | strings.ToUpper }}
但这不起作用,因为strings
不是我的数据的属性
我无法导入字符串
包,因为警告我未使用该包
下面是脚本:
只需使用类似的()将ToUpper函数注入到模板中即可
import (
"bytes"
"fmt"
"strings"
"text/template"
)
type TemplateData struct {
Name string
}
func main() {
funcMap := template.FuncMap{
"ToUpper": strings.ToUpper,
}
tmpl, _ := template.New("myTemplate").Funcs(funcMap).Parse(string("{{ .Name | ToUpper }}"))
templateDate := TemplateData{"Hello"}
var result bytes.Buffer
tmpl.Execute(&result, templateDate)
fmt.Println(result.String())
}
这并不能回答问题,但是:如果您使用此模板呈现HTML,请考虑大写字母是否只是表示形式。如果是,则使用CSS的
文本转换:大写
——这是一个更大的问题。