How to share data between middleware when using the middleware function of gorilla/mux

package main

import (
    "net/http"

    "github.com/gorilla/mux"
)

func main() {
    r := mux.NewRouter()
    
    r.Use(middleware1)
    r.Use(middleware2)
    
    r.HandleFunc("/user", getUser)
}

now I need to do some processing on token in middleware1, and then save the result and provide it to subsequent middleware. Where can I save it, or do I need to implement it in some other way?

Mar.09,2021

http.Request Context can be used to share data between middleware.


gorilla also has a package dedicated to context sharing data

Menu