package main import ( "github.com/go-martini/martini" "github.com/codegangsta/martini-contrib/render" ) type Person struct { Name string Age int } func main() { m := martini.Classic() m.Use(render.Renderer()) m.Get("/", func(r render.Render) { person := Person{Name: "John", Age: 25} r.HTML(200, "hello", person) }) m.Run() }
package main import ( "github.com/go-martini/martini" "github.com/codegangsta/martini-contrib/render" ) type Person struct { Name string `json:"name"` Age int `json:"age"` } func main() { m := martini.Classic() m.Use(render.Renderer()) m.Get("/json", func(r render.Render) { person := Person{Name: "John", Age: 25} r.JSON(200, person) }) m.Run() }In this example, we define the same `Person` struct with some additional `json` tags. We then create a new Martini instance and use the `render.Renderer()` middleware. In the `GET` handler for the `"/json"` route, we create a new `Person` instance and pass it to the `r.JSON()` function, which serializes the data to JSON and returns it as the response. Overall, the `github.com/codegangsta/martini-contrib.render` package provides a convenient way to render HTML templates and generate JSON responses in a Martini web application.