Import text/template, parse a template string with placeholders, and execute it with a data map to generate dynamic text.
Use the text/template package by importing it, parsing a template string, and executing it with data.
package main
import (
"os"
"text/template"
)
func main() {
tmpl, err := template.New("example").Parse("Hello, {{.Name}}!")
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, map[string]string{"Name": "World"})
if err != nil {
panic(err)
}
}
The text template package lets you create reusable text patterns with placeholders for dynamic data. You define the pattern once, then fill in the blanks with your specific information whenever you need it. Think of it like a mail-merge letter where you write the body once and just swap in the recipient's name for each copy.