4

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.

3
  • why not use negroni to handle middlewares Commented Mar 18, 2018 at 12:47
  • 2
    This code works for me. What is the version of Gorilla do you use? Commented Mar 18, 2018 at 12:47
  • Ha! You are right. Probably a very old one. I updatet my Gorilla package with go get -u and now it’s working. Many thanks!! Commented Mar 18, 2018 at 12:53

2 Answers 2

4

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!

Sign up to request clarification or add additional context in comments.

Comments

0

error is nil in ListenAndServe:

 http.ListenAndServe(":8000", r) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.