Last active
December 27, 2015 23:49
-
-
Save sanatgersappa/7408722 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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