我希望使用string.toupper在golang模板中大写一个字符串,如:
{{ .Name | strings.toupper }}
但这不起作用,因为字符串不是我数据的属性.
我无法导入字符串包,因为它警告我它没有被使用.
只需使用像这样的
FuncMap(
playground)将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()) }
原文地址:https://www.jb51.cc/go/186895.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。