请教一下: 在下面的一个模板string里面,从一个结构体里面传入一个name字符串变量。 tpl_str := "hello, {{.name}}" tpl_str += " {{.name}} is a machine。"
我想让传入的name变量在第一行所有字母都是大写的,怎么来操作呢?
先谢谢了!
有疑问加站长微信联系(非本文作者)

请教一下: 在下面的一个模板string里面,从一个结构体里面传入一个name字符串变量。 tpl_str := "hello, {{.name}}" tpl_str += " {{.name}} is a machine。"
我想让传入的name变量在第一行所有字母都是大写的,怎么来操作呢?
先谢谢了!
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
`单行代码`
正常做法 ,你需要写一个filter。
https://golang.org/pkg/text/template/#FuncMap
temlate文档中本身也有
我是新手,能否详细指点一下??
我给的链接下面就是 example啊,这个也需要我贴么……
package main
import ( "log" "os" "strings" "text/template" )
func main() { // First we create a FuncMap with which to register the function. funcMap := template.FuncMap{ // The name "title" is what the function will be called in the template text. "title": strings.Title, }
Input: {{printf "%q" .}} Output 0: {{title .}} Output 1: {{title . | printf "%q"}} Output 2: {{printf "%q" . | title}} `
}
template里可以传入FuncMap,里面是转换函数。
然后在模板里调用转换函数转换
谢谢了,问题已经解决了。 我这边访问不了golang.org呢
那你必须找个方法访问,不然怎么做后续的开发啊……