Skip to content

Commit 1bd7422

Browse files
committed
README: Update ParamsFromContext documentation
1 parent a18f223 commit 1bd7422

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func main() {
6464

6565
As you can see, `:name` is a *named parameter*. The values are accessible via `httprouter.Params`, which is just a slice of `httprouter.Param`s. You can get the value of a parameter either by its index in the slice, or by using the `ByName(name)` method: `:name` can be retrieved by `ByName("name")`.
6666

67-
**Note:** Since go 1.7 you may also access named parameters from `request.Context` if you're using the `http.Handler` api. See more below under `Why doesn't this work with http.Handler?`.
67+
When using a `http.Handler` (using `router.Handler` or `http.HandlerFunc`) instead of HttpRouter's handle API using a 3rd function parameter, the named parameters are stored in the `request.Context`. See more below under [Why doesn't this work with http.Handler?](#why-doesnt-this-work-with-httphandler).
6868

6969
Named parameters only match a single path segment:
7070

@@ -132,16 +132,18 @@ For even better scalability, the child nodes on each tree level are ordered by p
132132

133133
**It does!** The router itself implements the `http.Handler` interface. Moreover the router provides convenient [adapters for `http.Handler`](https://godoc.org/github.com/julienschmidt/httprouter#Router.Handler)s and [`http.HandlerFunc`](https://godoc.org/github.com/julienschmidt/httprouter#Router.HandlerFunc)s which allows them to be used as a [`httprouter.Handle`](https://godoc.org/github.com/julienschmidt/httprouter#Router.Handle) when registering a route.
134134

135-
~~The only disadvantage is, that no parameter values can be retrieved when a `http.Handler` or `http.HandlerFunc` is used, since there is no efficient way to pass the values with the existing function parameters. Therefore [`httprouter.Handle`](https://godoc.org/github.com/julienschmidt/httprouter#Router.Handle) has a third function parameter.~~
136-
137-
Since go 1.7 you can access named params from `request.Context`.
135+
Named parameters can be accessed `request.Context`:
138136

139137
```go
140-
params := r.Context().Value(httprouter.ParamsKey)
141-
// or use the helper
142-
params := httprouter.ParamsFromContext(r.Context())
138+
func Hello(w http.ResponseWriter, r *http.Request) {
139+
params := httprouter.ParamsFromContext(r.Context())
140+
141+
fmt.Fprintf(w, "hello, %s!\n", params.ByName("name"))
142+
}
143143
```
144144

145+
Alternatively, one can also use `params := r.Context().Value(httprouter.ParamsKey)` instead of the helper function.
146+
145147
Just try it out for yourself, the usage of HttpRouter is very straightforward. The package is compact and minimalistic, but also probably one of the easiest routers to set up.
146148

147149
## Where can I find Middleware *X*?

0 commit comments

Comments
 (0)