# Using the render package
If you want rendering JSON and HTML to be even simpler, there is the`github.com/unrolled/render` package. This package was inspired by the`martini-contrib/render` package and is my goto when it comes to rendering datafor presentation in my web applications.
~~~
package main
import (
"net/http"
"gopkg.in/unrolled/render.v1"
)
func main() {
r := render.New(render.Options{})
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Welcome, visit sub pages now."))
})
mux.HandleFunc("/data", func(w http.ResponseWriter, req *http.Request) {
r.Data(w, http.StatusOK, []byte("Some binary data here."))
})
mux.HandleFunc("/json", func(w http.ResponseWriter, req *http.Request) {
r.JSON(w, http.StatusOK, map[string]string{"hello": "json"})
})
mux.HandleFunc("/html", func(w http.ResponseWriter, req *http.Request) {
// Assumes you have a template in ./templates called "example.tmpl"
// $ mkdir -p templates && echo "<h1>Hello {{.}}.</h1>" > templates/example.tmpl
r.HTML(w, http.StatusOK, "example", nil)
})
http.ListenAndServe(":8080", mux)
}
~~~
## Exercises
1. Have fun playing with all of the options available when calling `render.New()`
1. Try using the `.yield` helper function (with the curly braces) and a layout with HTML templates.
- 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