« Back to Index

Generic Status Handler Abstraction

View original Gist on GitHub

Tags: #go #http

status_handler_abstraction.go

type StatusHandler int

func (s StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	code := int(s)
	w.WriteHeader(code)
	io.WriteString(w, http.StatusText(code))
}

var (
	NotFoundHandler = StatusHandler(404)
	NotImplementedHandler = StatusHandler(501)
	NotLegalHandler = StatusHandler(451)
)