# Tips and Tricks
## Wrap a http.HandlerFunc closure
Sometimes you want to pass data to a http.HandlerFunc on initialization. Thiscan easily be done by creating a closure of the `http.HandlerFunc`:
~~~
func MyHandler(database *sql.DB) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
// you now have access to the *sql.DB here
})
}
~~~
## Using `gorilla/context` for request-specific data
It is pretty often that we need to store and retrieve data that is specific tothe current HTTP request. Use `gorilla/context` to map values and retrieve themlater. It contains a global mutex on a map of request objects.
~~~
func MyHandler(w http.ResponseWriter, r *http.Request) {
val := context.Get(r, "myKey")
// returns ("bar", true)
val, ok := context.GetOk(r, "myKey")
// ...
}
~~~
- Introduction
- 1. Go Makes Things Simple
- 2. The net/http package
- 3. Creating a Basic Web App
- 4. Deployment
- 5. URL Routing
- 6. Middleware
- 7. Rendering
- JSON
- HTML Templates
- Using The render package
- 8. Testing
- Unit Testing
- End to End Testing
- 9. Controllers
- 10. Databases
- 11. Tips and Tricks
- 12. Moving Forward