路由执行体

Tango 支持 5 种形式的函数或结构体方法作为执行体:

  • func()
  • func(http.ResponseWriter, *http.Request)
  • func(*tango.Context)
  • func(http.Response.Writer)
  • func(*http.Request)
  • struct.Get()

  • func()

    1. t := tango.Classic()
    2. t.Get("/", func() string {
    3. return "hello tango"
    4. })
  • func(http.ResponseWriter, *http.Request)

    1. t := tango.Classic()
    2. t.Post("/", func(w http.ResponseWriter, *http.Request) {
    3. w.Write([]byte("hello tango"))
    4. })
  • func(http.ResponseWriter)

    1. t := tango.Classic()
    2. t.Post("/", func(w http.ResponseWriter) {
    3. w.Write([]byte("hello tango"))
    4. })
  • func(*http.Request)

    1. t := tango.Classic()
    2. t.Post("/", func(req *http.Request) string {
    3. return req.FormValue("key")
    4. })
  • func(*tango.Context)

    1. t := tango.Classic()
    2. t.Get("/:name", func(ctx *tango.Context) {
    3. ctx.Write([]byte("hello " + ctx.Params().Get(":name")))
    4. })
  • structs

    1. type Action struct {}
    2. func (a *Action) Get() string {
    3. return "haha"
    4. }
    5. t := tango.Classic()
    6. t.Get("/:name", new(Action))