How to use html template package

Import the html/template package from the Go standard library and use template.New().Parse() to create safe, dynamic HTML content.

The html/template package is not a standalone package; it is a subpackage of the standard library's text/template system used for safely rendering HTML. Import it using import "html/template" and parse your template string with template.New followed by Parse.

package main

import (
	"html/template"
	"log"
	"os"
)

func main() {
	tmpl, err := template.New("example").Parse("<h1>Hello, {{.Name}}!</h1>")
	if err != nil {
		log.Fatal(err)
	}
	data := map[string]string{"Name": "World"}
	err = tmpl.Execute(os.Stdout, data)
	if err != nil {
		log.Fatal(err)
	}
}