如何编写处理来自API的响应/错误的golang中间件?

Basically, I want to write one middleware which closes transaction object is created while a request. I am using gorilla mux package. I am familiar with python-Django middlewares and it gives proper handling of error or response. but not able to find anything similar with golang

In Go lang you can create a middle-ware too, below I've written the process to create handler as validateMiddleware then called it when TestEndpoint API requested.

func main() {
    router := mux.NewRouter()

    router.HandleFunc("/test", ValidateMiddleware(TestEndpoint)).Methods("GET")
    log.Fatal(http.ListenAndServe(":12345", router))
}

And now you can create your validateMiddleware handler as:

    func ValidateMiddleware(next http.HandlerFunc) http.HandlerFunc {
        return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
            authorizationHeader := req.Header.Get("authorization")
                    if authorizationHeader != "" {
                        // if true, then request for next handler.
                        next(w, req)
                    } else {
                        json.NewEncoder(w).Encode(Exception{Message: "Invalid authorization token"})
                        return
                    }
        })
    }

And finally create original requested handler TestEndpoint

func TestEndpoint(w http.ResponseWriter, req *http.Request) {
    fmt.Println("Hello Go middleware!!!")
}