December 2017
Intermediate to advanced
316 pages
6h 58m
English
We can define the preceding route in two steps:
r := mux.NewRouter()
r.Path("/articles/{category}/{id:[0- 9]+}").HandlerFunc(ArticleHandler) //chaining is possible
Be aware that the method chained here is HandlerFunc and not HandleFunc, as shown in the preceding code. We can create a top-level path and add subpaths to different handlers easily in Mux using Subrouter:
r := mux.NewRouter()s := r.PathPrefix("/articles").Subrouter()s.HandleFunc("{id}/settings", settingsHandler)s.HandleFunc("{id}/details", detailsHandler)
So all the URLs of the form http://localhost:8000/articles/123/settings redirect to settingsHandler and URLs of the form http://localhost:8000/articles/123/details ...
Read now
Unlock full access