Skip to main content
Go offers built-in support for creating dynamic content or showing customized output with the text/template package. A sibling package named html/template provides the same API but has additional security features.
Use html/template instead of text/template when generating HTML to prevent injection attacks.

Basic Templates

Create a template and parse its body from a string. Templates are a mix of static text and “actions” enclosed in {{...}}:
t1 := template.New("t1")
t1, err := t1.Parse("Value is {{.}}\n")
if err != nil {
    panic(err)
}

Using template.Must

Use template.Must to panic in case Parse returns an error. This is especially useful for templates initialized in the global scope:
t1 = template.Must(t1.Parse("Value: {{.}}\n"))

Executing Templates

Execute the template to generate text with specific values. The {{.}} action is replaced by the value passed to Execute:
t1.Execute(os.Stdout, "some text")
// Output: Value: some text

t1.Execute(os.Stdout, 5)
// Output: Value: 5

t1.Execute(os.Stdout, []string{"Go", "Rust", "C++", "C#"})
// Output: Value: [Go Rust C++ C#]

Accessing Fields

Struct Fields

Access struct fields using {{.FieldName}}. Fields must be exported:
t2 := template.Must(template.New("t2").Parse("Name: {{.Name}}\n"))

t2.Execute(os.Stdout, struct {
    Name string
}{"Jane Doe"})
// Output: Name: Jane Doe

Map Fields

The same applies to maps, with no restriction on key name case:
t2.Execute(os.Stdout, map[string]string{
    "Name": "Mickey Mouse",
})
// Output: Name: Mickey Mouse

Conditional Execution

Use if/else for conditional execution. A value is considered false if it’s the default value of a type:
t3 := template.Must(template.New("t3").Parse(
    "{{if . -}} yes {{else -}} no {{end}}\n"))

t3.Execute(os.Stdout, "not empty")
// Output: yes

t3.Execute(os.Stdout, "")
// Output: no
The - in actions trims whitespace around the action.

Range Loops

Loop through slices, arrays, maps, or channels. Inside the range block, {{.}} is set to the current item:
t4 := template.Must(template.New("t4").Parse(
    "Range: {{range .}}{{.}} {{end}}\n"))

t4.Execute(os.Stdout, []string{"Go", "Rust", "C++", "C#"})
// Output: Range: Go Rust C++ C#

Template Actions

References the current context value
Accesses a field or map key
Conditionally renders content
Iterates over collections
Trims surrounding whitespace

Package Reference

Build docs developers (and LLMs) love