How to Use text/template in Go

Web
Use text/template to parse a template string and execute it with data to generate dynamic text output.

Use the text/template package to parse a template string, execute it with data, and write the result to an output stream. This example defines a template, parses it, and executes it with a map of data.

package main

import (
	"os"
	"text/template"
)

func main() {
	tmpl := `Hello, {{.Name}}!`
	t, err := template.New("greeting").Parse(tmpl)
	if err != nil {
		panic(err)
	}
	data := map[string]string{"Name": "World"}
	err = t.Execute(os.Stdout, data)
	if err != nil {
		panic(err)
	}
}