Skip to content

Instantly share code, notes, and snippets.

@matryer
Created August 1, 2017 12:15
Show Gist options
  • Save matryer/2279f71005b4894df3e6dbb266f14b0f to your computer and use it in GitHub Desktop.
Save matryer/2279f71005b4894df3e6dbb266f14b0f to your computer and use it in GitHub Desktop.
Functions that return handlers let you neatly set things up
func handleGreeter() http.Handler {
// TODO: do any setup stuff here
//... like prepare a template:
tpl, err := template.ParseFiles("pages/_layout.html", "pages/gopher.html")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err != nil {
// parsing the template failed?
http.Error(w, err.Error(), http.StatusInternalServerError)
}
//... now use the template here!
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment