Mastering Go Templates: A Guide with Practical Examples
Go is a statically typed, compiled programming language designed by Google. It's known for its simplicity, efficiency, and robustness. Among its many features, Go's template package (text/template
and html/template
) is a powerful tool for generating textual output (like HTML) with dynamic data. This blog post will guide you through the basics of using Go templates, peppered with practical code examples.
Introduction to Go Templates
Go templates are a method for rendering dynamic content. They are particularly useful for web applications where you need to generate HTML dynamically. The standard library provides two template packages:
text/template
: For generating textual outputs like emails, configuration files, etc.html/template
: For generating HTML output, which automatically secures your application against certain attacks.
Syntax Basics
A template is a string or a file containing a mix of static data and actions enclosed in {{
and }}
. Actions inject dynamic data and control the flow of the template.
const tmpl = `Hello, {{.Name}}!`
In this example, {{.Name}}
is an action that will be replaced by the value of the Name
field from the passed data.
Setting Up a Simple Template
Creating a Template
Here’s how you can create and parse a template:
package main
import (
"text/template"
"os"
)
func main() {
const tmpl = `Hello, {{.Name}}!`
t, err := template.New("greet").Parse(tmpl)
if err != nil {
panic(err)
}
data := struct{ Name string }{"World"}
err = t.Execute(os.Stdout, data)
if err != nil {
panic(err)
}
}
This program will output: Hello, World!
HTML Template Example
When generating HTML, use the html/template
package to avoid cross-site scripting (XSS) vulnerabilities:
package main
import (
"html/template"
"os"
)
func main() {
const tmpl = `<h1>Hello, {{.Name}}!</h1>`
t, err := template.New("webpage").Parse(tmpl)
if err != nil {
panic(err)
}
data := struct{ Name string }{"World"}
err = t.Execute(os.Stdout, data)
if err != nil {
panic(err)
}
}
Using Template Functions
You can enhance templates with custom functions. Here's an example:
package main
import (
"text/template"
"os"
"strings"
)
func main() {
funcMap := template.FuncMap{
"title": strings.Title,
}
const tmpl = `Hello, {{title .Name}}!`
t, err := template.New("greet").Funcs(funcMap).Parse(tmpl)
if err != nil {
panic(err)
}
data := struct{ Name string }{"world"}
err = t.Execute(os.Stdout, data)
if err != nil {
panic(err)
}
}
This will output: Hello, World!
, with World
capitalized using the title
function.
Range and Conditional Statements
Templates support looping and conditional logic. Here's an example using range
:
const tmpl = `
{{range .}}
Hello, {{.}}!
{{end}}
`
And here's one with if
:
const tmpl = `
{{if .IsAdmin}}
Welcome, administrator!
{{else}}
Welcome, user!
{{end}}
`
These structures add powerful control flows to your templates.
Go templates are an efficient way to generate dynamic textual content. Whether you're developing a web application and need to generate HTML on the fly, or you're creating text-based outputs like emails or reports, Go templates offer a robust and secure solution. Remember to always use html/template
for HTML content to ensure your application's security.