Skip to content

Instantly share code, notes, and snippets.



Last active Mar 8, 2017
What would you like to do?
Simple, re-usable handlers for standard tasks.
package main
import (
func serveFile(filename string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filename)
func fileServer(dirname string) http.Handler {
return http.FileServer(http.Dir(dirname))
func redirect(path string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, path, http.StatusFound)
func notFound(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
func internalServerError(w http.ResponseWriter, err error) {
log.Printf("Err: %s\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
func render(w http.ResponseWriter, tmpl *template.Template, tmplName string, data interface{}) {
buf := &bytes.Buffer{}
err := tmpl.ExecuteTemplate(buf, tmplName, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment