Tango supports 4 forms path:
tg.Get("/", new(Action))
tg.Get("/static", new(Action))
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))
tg.Get("/*name", new(Action))
tg.Get("/ttt/*name", new(Action))
tg.Get("/sss(*name)", new(Action))
tg.Get("/(:name.*)", new(Action))
tg.Get("/(:name[0-9]+)", new(Action))
tg.Get("/(:name1)-(:name2[0-9]+)", new(Action))
t.Route("GET", "/", new(Action))
type Action struct {}
func (Action) MyPost() {}
t.Route("POST:MyPost", "/", new(Action))
t.Route([]string{"GET:MyGet", "POST"}, "/", new(Action))
t.Route(map[string]string{"GET":"MyGet", "POST":"MyPost"}, "/", new(Action))
For examples:
t := tango.Classic()
t.Get("/:name", new(Others))
t.Get("/admin", new(Admin))
t.Run()
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))
t.Run()
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))
t.Run()
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 or regexp route: ctx.Param(":name")
或者 ctx.Param("name")
* For catch-all route: ctx.Param("*name")
import "gitea.com/tango/binding"
type Getuser struct {
tango.JSON
tango.Ctx
binding.Binder
}
func (this *Getuser) Get() interface{} {
type GetStruct struct {
Name string
}
var data GetStruct
err := this.Bind(&data)
if err != nil {
return map[string]interface{}{
"res": "no",
"msg": "get param error",
"err:": err,
"status": 500,
}
}
fmt.Println(data.Name)
//.....
}
func main() {
t := tango.Classic()
t.Use(binding.Bind())
t.Get("/api/1.0/users", new(Getuser))
//......
type Settid struct {
tango.JSON
tango.Ctx
}
func (this *Settid) Post() interface{} {
type jsondata struct {
Uid string `json:"Uid"`
Tid string `json:"Tid"`
Aim_sn string `json:"Aim_sn"`
}
var data jsondata
err = this.DecodeJSON(&data)
if err != nil {
return map[string]interface{}{
"res": "no",
"msg": "DecodeJSON error",
"err:": err,
"status": 500,
}
}
fmt.Println(data.Uid)
//.....
}
func main() {
t := tango.Classic()
t.Post("/inapi/1.0/setaiport", new(Settid))
//......