Go

text/template — Simple Templating

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Stdlib templating with `{{.Field}}` placeholders, range loops, if/else, and function pipelines. Safe alternative for non-HTML text (e.g. emails, config files).
Go
Raw
package main

import (
    "os"
    "text/template"
)

const emailTmpl = `Hi {{.Name}},

Thanks for signing up to {{.Product}}! You're on the {{.Plan}} plan.

{{if .Trial}}You have {{.TrialDays}} days of free trial.
{{end -}}
Visit https://{{.Product | lower}}.com/dashboard to get started.
`

func main() {
    funcs := template.FuncMap{
        "lower": strings.ToLower,
    }
    t := template.Must(template.New("email").Funcs(funcs).Parse(emailTmpl))

    data := struct {
        Name, Product, Plan string
        Trial               bool
        TrialDays           int
    }{
        Name: "Alice", Product: "MyApp", Plan: "Pro",
        Trial: true,   TrialDays: 14,
    }

    t.Execute(os.Stdout, data)
}

// import strings — omitted above for brevity
import "strings"
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.