package main import ( "html/template" "log" "net/http" ) var x = 2 func main() { http.HandleFunc("/data", func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(200) const tpl = `

Hey check out the data

` t, err := template.New("default").Parse(tpl) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = t.Execute(w, nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }) http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(200) // fmt.Fprintf(w, "Welcome to the home page!") const tpl = ` {{.Title}} {{range .Items}}
{{ . }}
{{else}}
no rows
{{end}} ` t, err := template.New("default").Parse(tpl) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } data := struct { Title string Items []string }{ Title: "Hello", Items: []string{"yo", "what's up"}, // template automatically escapes it } err = t.Execute(w, data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }) log.Printf("listening on port 8080") log.Fatal(http.ListenAndServe(":8080", nil)) }