Go Web Examples: Templates

This example will show how to render a simple list of TODO items into an html page using the html/template package.

// todos.go
package main

import (

type Todo struct {
	Task string
	Done bool

func main() {
	tmpl := template.Must(template.ParseFiles("todos.html"))
	todos := []Todo{
		{"Learn Go", true},
		{"Read Go Web Examples", true},
		{"Create a web app in Go", false},

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		tmpl.Execute(w, struct{ Todos []Todo }{todos})

	http.ListenAndServe(":8080", nil)
<!-- todos.html -->
	{{range .Todos}}
		{{if .Done}}
$ go run todos.go