Maintaining context
Throughout early Go web application development, there was much contention surrounding how to weave together context when chaining handler function calls together. If we examine the standard library http.HandlerFunc
definition, type HandlerFunc func(ResponseWriter, *Request)
, this type is not conducive for building a chained middleware scheme, or calling other request handlers from within handlers whatsoever. This is because of the two different parameters, http.ResponseWriter
and *http.Request
. The http.ResponseWriter
is an interface that is designed primarily to write a sequence of bytes back to the caller, whereas the second parameter, the *http.Request
, is a pointer to the data structure that represents the request.
Prior to Go 1.7, there was no mechanism in place within the http.Request
type to store extra information to keep the state from one nested handler function call to the next. This absence of a means of keeping context, or extra information created from the...