Skip to content

Instantly share code, notes, and snippets.

@sanatgersappa
Last active December 27, 2015 23:49
Show Gist options
  • Save sanatgersappa/7408722 to your computer and use it in GitHub Desktop.
Save sanatgersappa/7408722 to your computer and use it in GitHub Desktop.
package main
import (
"html/template"
"net/http"
)
//Compile templates on start
var templates = template.Must(template.ParseFiles("header.html", "footer.html", "main.html", "about.html"))
//A Page structure
type Page struct {
Title string
}
//Display the named template
func display(w http.ResponseWriter, tmpl string, data interface{}) {
templates.ExecuteTemplate(w, tmpl, data)
}
//The handlers.
func mainHandler(w http.ResponseWriter, r *http.Request) {
display(w, "main", &Page{Title: "Home"})
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
display(w, "about", &Page{Title: "About"})
}
func main() {
http.HandleFunc("/", mainHandler)
http.HandleFunc("/about", aboutHandler)
//Listen on port 8080
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment