Unwrap exposes an underlying wrapped object, especially an http.ResponseWriter, so handlers can recover optional behavior hidden by middleware or embedding. section 9.4.1 section 9.4.3
Use Unwrap to preserve behavior across ResponseWriter wrappers:
- Implement Unwrap on a wrapper and return its embedded ResponseWriter. section 9.4.3
- Let handlers or helpers discover Unwrap with a type assertion before accessing the original writer. section 9.8.2
- Repeat unwrapping through middleware layers until no Unwrap method remains. section 9.8.2
- Use the recovered writer when optional behavior such as Flush or connection disconnection depends on the original implementation. section 9.4.3 section 9.8.2
Expose the embedded writer
func (ic *Interceptor) Unwrap() http.ResponseWriter { return ic.ResponseWriter } This lets wrapped handlers reach optional interfaces such as http.Flusher. For newer code, http.NewResponseController(w).Flush() can search the wrapper chain and report http.ErrNotSupported when flushing is unavailable. section 9.4.3 section 9.4.2
Listing 9.27 Unwrapping ResponseWriters (link/kit/hio/request.go)
// MaxBytesReader is like [http.MaxBytesReader], but unwraps the
// original [http.ResponseWriter] if it's wrapped.
func MaxBytesReader(
w http.ResponseWriter, rc io.ReadCloser, max int64,
) io.ReadCloser {
type unwrapper interface { #1
Unwrap() http.ResponseWriter #1
} #1
for { #2
v, ok := w.(unwrapper) #3
if !ok {
break #4
}
w = v.Unwrap() #5
}
return http.MaxBytesReader(w, rc, max)
}
A recursive MaxBytesReader helper demonstrates how to peel away middleware wrappers before passing the original ResponseWriter to the HTTP server. section 9.8.2
