Lesson 20 of 60 · Go
Template Rendering with `html/template`
Duration: 15 minutes
HTML Templates
This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.
html/template auto‑escapes content, preventing XSS.
tmpl := template.Must(template.ParseFiles("index.html"))
type PageData struct { Title string; Items []string }
func handler(w http.ResponseWriter, r *http.Request) {
data := PageData{Title: "My Site", Items: []string{"Go", "Docker", "K8s"}}
tmpl.Execute(w, data)
}
Template syntax basics
<h1>{{ .Title }}</h1>
<ul>
{{ range .Items }}
<li>{{ . }}</li>
{{ end }}
</ul>