Gorilla Mux Use() 函数不工作

Gorilla Mux Use() Function Not Working

我想使用 Gorilla Mux 包的 Use() 函数,但我无法让它工作。它说:r.Use undefined (type *mux.Router has no field or method Use)。我使用了文档中的 almot identitcal 示例。我的代码看起来像这样。

package main

import (
    "net/http"
    "github.com/gorilla/mux"
    "fmt"
)

func simpleMw(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Println(r.RequestURI)
        next.ServeHTTP(w, r)
    })
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "hello")
}

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", handler)
    r.Use(simpleMw)
    http.Handle("/", r)
    http.ListenAndServe(":8000", nil)
}

您可以在此处找到文档示例:http://www.gorillatoolkit.org/pkg/mux#overview,搜索 "middleware"。

我知道我可以使用 this 方法,但我想使用 Gorilla 包。

非常感谢。

感谢 Ivan Velichko,我解决了我的问题。我的包裹已经过时了。我用 go get -u github.com/gorilla/mux 更新了它,现在它可以工作了。非常感谢大家!