Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go Gorilla Mux "match anything" path template

Tags:

go

gorilla

What is the proper syntax to create a simple "match anything" handler?

mux.NewRouter().StrictSlash(true).Path("/")....

The above code seems to strictly match / and /foo won't get matched

like image 326
frankgreco Avatar asked Apr 14 '17 18:04

frankgreco


2 Answers

This should work:

router := mux.NewRouter().PathPrefix("/")
like image 158
Cyril Graze Avatar answered Oct 17 '22 19:10

Cyril Graze


You can use mux.Route.HandlerFunc together with mux.Router.PathPrefix:

r := mux.NewRouter()

// route catalog to catalogHandler:
r.HandleFunc("/catalog/{id}", catalogHandler) 

// route everything else to defaultHandler:
r.PathPrefix("/").HandlerFunc(defaultHandler)

Note the difference in names (HandlerFunc vs HandleFunc).

like image 9
Dmitry Mottl Avatar answered Oct 17 '22 18:10

Dmitry Mottl