Created
August 1, 2017 12:15
-
-
Save matryer/2279f71005b4894df3e6dbb266f14b0f to your computer and use it in GitHub Desktop.
Functions that return handlers let you neatly set things up
This file contains hidden or 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
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