Skip to content

Instantly share code, notes, and snippets.

@tenntenn
Created October 14, 2013 04:33
Show Gist options
  • Save tenntenn/6970823 to your computer and use it in GitHub Desktop.
Save tenntenn/6970823 to your computer and use it in GitHub Desktop.
[Go言語] httpハンドラの共通部分を取り出していい感じにする ref: http://qiita.com/tenntenn/items/b7bd54c7ba0ff90f1707
package main
import (
"fmt"
"log"
"net/http"
)
func baseHandlerFunc(handler func(w http.ResponseWriter, r *http.Request)) http.Handler {
return baseHandler(http.HandlerFunc(handler))
}
func baseHandler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// common
log.Println(r.URL, r.Method)
handler.ServeHTTP(w, r)
})
}
// handler
func index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello")
}
func main() {
http.Handle("/", baseHandlerFunc(index))
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment