The first half of this answer demonstrates a pattern for sharing some per-request context between two handler methods. Sort of.
I'm looking for a way to do basically this, but with more than two handlers.
Imagine I need to call three (or more) http.HandlerFunc
s with the same per-request context:
Without resorting to a global map (e.g. gorilla/mux), is there any way, using the standard http.Handler
interface, to pass per-request context between these functions?
You can cheat and wrap the Request.Body (a ReaderCloser interface) with your context.
Here's an example: webctx.go
when you want your context, just type assert request.Body to be your type. Only trick is your type must hold the original body and implement the ReaderCloser methods... but that's a minimal amount of code.