I don't think this is a good idea.
I would do something like this.
type mykey struct{}
var key mykey
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var s string
r = r.WithContext(context.WithValue(r.Context(), &key, &s))
next.ServeHTTP(w, r)
if s != "" {
fmt.Fprint("string %s\n", s)
}
})
})
In the handler itself you must of course get the value by assuming that key will always have string pointer. You can then assign a string to the pointer, which will be output if its non-nil. Wonder whether you want to store a map or a io.Writer as value instead of a single string.