Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Go Programming Bootcamp

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>

Info

Always use html/template (instead of text/template) for rendering user‑generated HTML.

Previous: JSON Marshalling & UnmarshallingNext: Building a RESTful API – CRUD Operations