Golang template : Use pipe to uppercase string
go, string, templates, uppercase
Solution
Just use a FuncMap like this (playground) to inject the ToUpper function into your template.
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())
}
Problem
I want to upper case a string in a golang template using `string.ToUpper` like : ``` {{ .Name | strings.ToUpper }} ``` But this doesn't works because `strings` is not a property of my data. I can't import `strings` package because the warns me that it's not used. Here the script : http://play.golang.org/p/7D69Q57WcN