Skip to content

Commit

Permalink
[docs] Add route listing example to README
Browse files Browse the repository at this point in the history
* Update README.md

* Update README.md
  • Loading branch information
shawnps authored and elithrar committed Jan 18, 2017
1 parent b128961 commit cafdb65
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 23,7 @@ The name mux stands for "HTTP request multiplexer". Like the standard `http.Serv
* [Install](#install)
* [Examples](#examples)
* [Matching Routes](#matching-routes)
* [Listing Routes](#listing-routes)
* [Static Files](#static-files)
* [Registered URLs](#registered-urls)
* [Full Example](#full-example)
Expand Down Expand Up @@ -167,6 168,42 @@ s.HandleFunc("/{key}/", ProductHandler)
s.HandleFunc("/{key}/details", ProductDetailsHandler)
```

### Listing Routes

Routes on a mux can be listed using the Router.Walk method—useful for generating documentation:

```go
package main

import (
"fmt"
"net/http"

"github.com/gorilla/mux"
)

func handler(w http.ResponseWriter, r *http.Request) {
return
}

func main() {
r := mux.NewRouter()
r.HandleFunc("/", handler)
r.HandleFunc("/products", handler)
r.HandleFunc("/articles", handler)
r.HandleFunc("/articles/{id}", handler)
r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
t, err := route.GetPathTemplate()
if err != nil {
return err
}
fmt.Println(t)
return nil
})
http.Handle("/", r)
}
```

### Static Files

Note that the path provided to `PathPrefix()` represents a "wildcard": calling
Expand Down

0 comments on commit cafdb65

Please sign in to comment.