gogorillamux

Gorilla Mux Use() Function Not Working


I want to use the Use() function of the Gorilla Mux package, but I cannot get it to work. It says: r.Use undefined (type *mux.Router has no field or method Use). I used almot the identitcal example from the documentation. My code looks like this.

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)
}

You can find the example of the documentation here: http://www.gorillatoolkit.org/pkg/mux#overview, search for "middleware".

I know I could use this method, but I would like to use the Gorilla package.

Many thanks.


Solution

  • Thanks to Ivan Velichko, I solved my problem. My package was outdated. I updated it with go get -u github.com/gorilla/mux and now it’s working. Many thanks to y’all!