package whoisthis import ( "context" "net/http" ) type CtxKey string const ContextUserId CtxKey = "userid" func (wis WhoIsThis) Authed(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Our middleware logic goes here... user, err := wis.LookupReqFn(r) if err != nil { http.Error(w, "please sign-in", http.StatusUnauthorized) return } ctxWithUser := context.WithValue(r.Context(), ContextUserId, user) rWithUser := r.WithContext(ctxWithUser) next.ServeHTTP(w, rWithUser) }) }