Getting Started with Golang text/template
Getting Started with Golang text/template
August 5, 2023
Template package
First example
- It outputs the result to Stdout
- Insert variables into a template
Use Go playground
{% raw %}
type TemplateVariables struct {
Name string
}
tmpl := template.Must(template.New("template").Parse(`{{ .Name }}`))
err := tmpl.Execute(os.Stdout, TemplateVariables{
Name: "name inserted",
})
if err != nil {
return err
}
return nil{% endraw %}
Define a custom function
Use template.FuncMap and template.Template.Func to define a Custom Func.
In order to use the function in a template, just use that function
{% raw %}
type TemplateVariables struct {
Name string
}
tmpl := template.Must(template.New("template").Funcs(template.FuncMap{
"CustomFunc": func(arg string) string {
return "return " + arg
},
}).Parse(`{{ .Name }}
{{ CustomFunc "argument" }}`))
err := tmpl.Execute(os.Stdout, TemplateVariables{
Name: "name inserted",
})
if err != nil {
return err
}
return nil{% endraw %}
Read a file
Use template.Template.ParseFiles or template.ParseFiles
If you use template.Template.ParseFiles, then use the base name of a template file.
Template features
Basic syntax
- To access an element of a slice by an index, use
index $var $index
To extend a template like inheritance
- Use
blockortemplateon a base file, anddefinein the main file - Pass these two files to
template.Template.ParseFiles - Use the base filename for
template.New - In the
define, the variables defined outside of the block cannot be used. It’s out of scope
It was not possible to define a variable in the main file and use it on the base file, probably.
Last updated on