函数式编程
如果您想使用我发现很常见的默认多路复用器,您可以像这样创建中间件:func mustBeLoggedIn(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { // Am i logged in? if ...not logged in... { http.Error(w, err.Error(), http.StatusUnauthorized) return } // Pass through to the original handler. handler(w, r) }}像这样使用它:http.HandleFunc("/some/priveliged/action", mustBeLoggedIn(myVanillaHandler))http.ListenAndServe(":80", nil)