|
6 | 6 | // |
7 | 7 | // A trivial example is: |
8 | 8 | // |
9 | | -// package main |
| 9 | +// package main |
10 | 10 | // |
11 | | -// import ( |
12 | | -// "fmt" |
13 | | -// "github.com/julienschmidt/httprouter" |
14 | | -// "net/http" |
15 | | -// "log" |
16 | | -// ) |
| 11 | +// import ( |
| 12 | +// "fmt" |
| 13 | +// "github.com/julienschmidt/httprouter" |
| 14 | +// "net/http" |
| 15 | +// "log" |
| 16 | +// ) |
17 | 17 | // |
18 | | -// func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { |
19 | | -// fmt.Fprint(w, "Welcome!\n") |
20 | | -// } |
| 18 | +// func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { |
| 19 | +// fmt.Fprint(w, "Welcome!\n") |
| 20 | +// } |
21 | 21 | // |
22 | | -// func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { |
23 | | -// fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name")) |
24 | | -// } |
| 22 | +// func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { |
| 23 | +// fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name")) |
| 24 | +// } |
25 | 25 | // |
26 | | -// func main() { |
27 | | -// router := httprouter.New() |
28 | | -// router.GET("/", Index) |
29 | | -// router.GET("/hello/:name", Hello) |
| 26 | +// func main() { |
| 27 | +// router := httprouter.New() |
| 28 | +// router.GET("/", Index) |
| 29 | +// router.GET("/hello/:name", Hello) |
30 | 30 | // |
31 | | -// log.Fatal(http.ListenAndServe(":8080", router)) |
32 | | -// } |
| 31 | +// log.Fatal(http.ListenAndServe(":8080", router)) |
| 32 | +// } |
33 | 33 | // |
34 | 34 | // The router matches incoming requests by the request method and the path. |
35 | 35 | // If a handle is registered for this path and method, the router delegates the |
|
39 | 39 | // |
40 | 40 | // The registered path, against which the router matches incoming requests, can |
41 | 41 | // contain two types of parameters: |
42 | | -// Syntax Type |
43 | | -// :name named parameter |
44 | | -// *name catch-all parameter |
| 42 | +// |
| 43 | +// Syntax Type |
| 44 | +// :name named parameter |
| 45 | +// *name catch-all parameter |
45 | 46 | // |
46 | 47 | // Named parameters are dynamic path segments. They match anything until the |
47 | 48 | // next '/' or the path end: |
48 | | -// Path: /blog/:category/:post |
49 | 49 | // |
50 | | -// Requests: |
51 | | -// /blog/go/request-routers match: category="go", post="request-routers" |
52 | | -// /blog/go/request-routers/ no match, but the router would redirect |
53 | | -// /blog/go/ no match |
54 | | -// /blog/go/request-routers/comments no match |
| 50 | +// Path: /blog/:category/:post |
| 51 | +// |
| 52 | +// Requests: |
| 53 | +// /blog/go/request-routers match: category="go", post="request-routers" |
| 54 | +// /blog/go/request-routers/ no match, but the router would redirect |
| 55 | +// /blog/go/ no match |
| 56 | +// /blog/go/request-routers/comments no match |
55 | 57 | // |
56 | 58 | // Catch-all parameters match anything until the path end, including the |
57 | 59 | // directory index (the '/' before the catch-all). Since they match anything |
58 | 60 | // until the end, catch-all parameters must always be the final path element. |
59 | | -// Path: /files/*filepath |
60 | 61 | // |
61 | | -// Requests: |
62 | | -// /files/ match: filepath="/" |
63 | | -// /files/LICENSE match: filepath="/LICENSE" |
64 | | -// /files/templates/article.html match: filepath="/templates/article.html" |
65 | | -// /files no match, but the router would redirect |
| 62 | +// Path: /files/*filepath |
| 63 | +// |
| 64 | +// Requests: |
| 65 | +// /files/ match: filepath="/" |
| 66 | +// /files/LICENSE match: filepath="/LICENSE" |
| 67 | +// /files/templates/article.html match: filepath="/templates/article.html" |
| 68 | +// /files no match, but the router would redirect |
66 | 69 | // |
67 | 70 | // The value of parameters is saved as a slice of the Param struct, consisting |
68 | 71 | // each of a key and a value. The slice is passed to the Handle func as a third |
69 | 72 | // parameter. |
70 | 73 | // There are two ways to retrieve the value of a parameter: |
71 | | -// // by the name of the parameter |
72 | | -// user := ps.ByName("user") // defined by :user or *user |
73 | 74 | // |
74 | | -// // by the index of the parameter. This way you can also get the name (key) |
75 | | -// thirdKey := ps[2].Key // the name of the 3rd parameter |
76 | | -// thirdValue := ps[2].Value // the value of the 3rd parameter |
| 75 | +// // by the name of the parameter |
| 76 | +// user := ps.ByName("user") // defined by :user or *user |
| 77 | +// |
| 78 | +// // by the index of the parameter. This way you can also get the name (key) |
| 79 | +// thirdKey := ps[2].Key // the name of the 3rd parameter |
| 80 | +// thirdValue := ps[2].Value // the value of the 3rd parameter |
77 | 81 | package httprouter |
78 | 82 |
|
79 | 83 | import ( |
@@ -246,7 +250,12 @@ func (r *Router) saveMatchedRoutePath(path string, handle Handle) Handle { |
246 | 250 | } |
247 | 251 | } |
248 | 252 |
|
249 | | -// GET is a shortcut for router.Handle(http.MethodGet, path, handle) |
| 253 | +// NewGroup adds a zero overhead group of routes that share a common root path. |
| 254 | +func (r *Router) NewGroup(path string) *RouteGroup { |
| 255 | + return newRouteGroup(r, path) |
| 256 | +} |
| 257 | + |
| 258 | +// GET is a shortcut for router.Handle("GET", path, handle) |
250 | 259 | func (r *Router) GET(path string, handle Handle) { |
251 | 260 | r.Handle(http.MethodGet, path, handle) |
252 | 261 | } |
@@ -366,7 +375,8 @@ func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc) { |
366 | 375 | // of the Router's NotFound handler. |
367 | 376 | // To use the operating system's file system implementation, |
368 | 377 | // use http.Dir: |
369 | | -// router.ServeFiles("/src/*filepath", http.Dir("/var/www")) |
| 378 | +// |
| 379 | +// router.ServeFiles("/src/*filepath", http.Dir("/var/www")) |
370 | 380 | func (r *Router) ServeFiles(path string, root http.FileSystem) { |
371 | 381 | if len(path) < 10 || path[len(path)-10:] != "/*filepath" { |
372 | 382 | panic("path must end with /*filepath in path '" + path + "'") |
|
0 commit comments