You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

doc.go 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*Package render is a package that provides functionality for easily rendering JSON, XML, binary data, and HTML templates.
  2. package main
  3. import (
  4. "encoding/xml"
  5. "net/http"
  6. "github.com/unrolled/render"
  7. )
  8. type ExampleXml struct {
  9. XMLName xml.Name `xml:"example"`
  10. One string `xml:"one,attr"`
  11. Two string `xml:"two,attr"`
  12. }
  13. func main() {
  14. r := render.New()
  15. mux := http.NewServeMux()
  16. mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
  17. w.Write([]byte("Welcome, visit sub pages now."))
  18. })
  19. mux.HandleFunc("/data", func(w http.ResponseWriter, req *http.Request) {
  20. r.Data(w, http.StatusOK, []byte("Some binary data here."))
  21. })
  22. mux.HandleFunc("/text", func(w http.ResponseWriter, req *http.Request) {
  23. r.Text(w, http.StatusOK, "Plain text here")
  24. })
  25. mux.HandleFunc("/json", func(w http.ResponseWriter, req *http.Request) {
  26. r.JSON(w, http.StatusOK, map[string]string{"hello": "json"})
  27. })
  28. mux.HandleFunc("/jsonp", func(w http.ResponseWriter, req *http.Request) {
  29. r.JSONP(w, http.StatusOK, "callbackName", map[string]string{"hello": "jsonp"})
  30. })
  31. mux.HandleFunc("/xml", func(w http.ResponseWriter, req *http.Request) {
  32. r.XML(w, http.StatusOK, ExampleXml{One: "hello", Two: "xml"})
  33. })
  34. mux.HandleFunc("/html", func(w http.ResponseWriter, req *http.Request) {
  35. // Assumes you have a template in ./templates called "example.tmpl".
  36. // $ mkdir -p templates && echo "<h1>Hello HTML world.</h1>" > templates/example.tmpl
  37. r.HTML(w, http.StatusOK, "example", nil)
  38. })
  39. http.ListenAndServe("127.0.0.1:3000", mux)
  40. }
  41. */
  42. package render