Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

html/template

  • html/template
  • Based on text/template so it has the same API.
  • Automatically escapes dangerous characters.
package main

import (
	"html/template"
	"os"
)

func main() {
	tmpl, err := template.New("test").Parse("Hello World!\n")
	if err != nil {
		panic(err)
	}
	err = tmpl.Execute(os.Stdout, nil)
	if err != nil {
		panic(err)
	}

	person := map[string]string{
		"Name": "Joe",
	}
	tmpl, err = template.New("test").Parse("Hello {{.Name}}\n")
	if err != nil {
		panic(err)
	}
	err = tmpl.Execute(os.Stdout, person)
	if err != nil {
		panic(err)
	}

	other := Person{Name: "Jane"}
	err = tmpl.Execute(os.Stdout, other)
	if err != nil {
		panic(err)
	}
}

type Person struct {
	Name string
}