Skip to content
/ respond Public

respond Text, HTML, XML, JSON, JSONP data to http.ResponseWriter. 响应不同格式的数据到HTTP客户端

License

Notifications You must be signed in to change notification settings

gookit/respond

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Respond

GoDoc Go Report Card Unit-Tests

Quickly respond Text, HTML, XML, JSON, JSONP and more data to http.ResponseWriter.

Godoc

Quick start

package main

import (
    "net/http"
	
    "github.com/gookit/respond"
)

func main() {
    // config and init the default Responder
    respond.Initialize(func(opts *respond.Options) {
        opts.TplLayout = "two-column.tpl"
        opts.TplViewsDir = "templates"
    })
    
    http.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
        respond.JSON(w, 200, map[string]string{
            "name": "tom",
        })
    })
    
    http.HandleFunc("/xml", func(w http.ResponseWriter, r *http.Request) {
        respond.XML(w, 200, map[string]string{
            "name": "tom",
        })
    })
    
    http.HandleFunc("/html", func(w http.ResponseWriter, r *http.Request) {
        respond.HTML(w, 200, "home.tpl", map[string]string{
            "name": "tom",
        })
    })
    
    http.ListenAndServe(":8080", nil)
}

Create new

package main

import (
    "net/http"
	
    "github.com/gookit/respond"
)

func main() {
    render := respond.New(func(opts *respond.Options) {
        opts.TplLayout = "two-column.tpl"
        opts.TplViewsDir = "templates"
    })
    render.Initialize()
    
    // usage
    http.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
        render.JSON(w, 200, map[string]string{
            "name": "tom",
        })
    })
    http.HandleFunc("/html", func(w http.ResponseWriter, r *http.Request) {
        render.HTML(w, 200, "home.tpl", map[string]string{
            "name": "tom",
        })
    })
    
}

Reference

License

MIT

About

respond Text, HTML, XML, JSON, JSONP data to http.ResponseWriter. 响应不同格式的数据到HTTP客户端

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages