12 Router
Lunny Xiao edited this page 2015-04-17 20:19:59 +08:00



Tango supports 4 forms path:

  • static path
tg.Get("/", new(Action))
tg.Get("/static", new(Action))
  • named path
tg.Get("/:name", new(Action))
tg.Get("/(:name)", new(Action))
tg.Get("/:name1/:name2", new(Action))
tg.Get("/:name1-:name2", new(Action))
tg.Get("/(:name1)sss(:name2)", new(Action))
  • catch-all path
tg.Get("/*name", new(Action))
tg.Get("/ttt/*name", new(Action))
tg.Get("/sss(*name)", new(Action))
  • regexp path
tg.Get("/(:name.*)", new(Action))
tg.Get("/(:name[0-9]+)", new(Action))
tg.Get("/(:name1)-(:name2[0-9]+)", new(Action))

Router Priority

  1. Static router is always first matched before other routers even it was added last.
  2. Other routers will order by it's added sequence.

For examples:

t := tango.Classic()
t.Get("/:name", new(Others))
t.Get("/admin", new(Admin))

When you request /admin, the Admin's Get method will be invoked.

t := tango.Classic()
t.Get("/:name", new(Admin))
t.Get("/*name", new(Others))

When you request /admin, the Admin's Get method will be invoked. When you request /admin/ui, Others's Get method will be invoked.

t := tango.Classic()
t.Get("/*name", new(Admin))
t.Get("/:name", new(Others))

Others's Get will never be invoked, because all matched requests will be matched to Admin's Get.


The matched params can get By *Context For named , catch-all or regexp Path: ctx.Params().Get(":name")

Funcs & Actions

Tango supports 5 forms functions or struct methods:

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


t := tango.Classic()
t.Get("/", func() string {
    return "hello tango"

func(http.ResponseWriter, *http.Request)

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


t := tango.Classic()
t.Post("/", func(w http.ResponseWriter) {
    w.Write([]byte("hello tango"))


t := tango.Classic()
t.Post("/", func(req *http.Request) string {
    return req.FormValue("key")


t := tango.Classic()
t.Get("/:name", func(ctx *tango.Context) {
    ctx.Write([]byte("hello " + ctx.Params().Get(":name")))


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

More routes support

GET route

t.Route("GET", "/", new(Action))

custom method route

type Action struct {}
func (Action) MyPost() {}

t.Route("POST:MyPost", "/", new(Action))

define more route once

t.Route([]string{"GET:MyGet", "POST"}, "/", new(Action))

define more route once via map

t.Route(map[string]string{"GET":"MyGet", "POST":"MyPost"}, "/", new(Action))