iris 获取路径中的参数

2022-03-26 10:58 更新
func main() {
    app := iris.Default()

    // This handler will match /user/john but will not match /user/ or /user
    app.Get("/user/{name}", func(ctx iris.Context) {
        name := ctx.Params().Get("name")
        ctx.Writef("Hello %s", name)
    })

    // However, this one will match /user/john/ and also /user/john/send
    // If no other routers match /user/john, it will redirect to /user/john/
    app.Get("/user/{name}/{action:path}", func(ctx iris.Context) {
        name := ctx.Params().Get("name")
        action := ctx.Params().Get("action")
        message := name + " is " + action
        ctx.WriteString(message)
    })

    // For each matched request Context will hold the route definition
    app.Post("/user/{name:string}/{action:path}", func(ctx iris.Context) {
        ctx.GetCurrentRoute().Tmpl().Src == "/user/{name:string}/{action:path}" // true
    })

    app.Listen(":8080")
}

内置可用参数类型

 参数类型  Go语言类型  验证  iris对应检索
 string  string  字符串  Params().Get
 uuid  string  uuid  Params().Get
 int  int -9223372036854775808 至 9223372036854775807  Params().GetInt
 int8  int8  -128 至 127  Params().GetInt8
 int16  int16  -32768 至 32767  Params().GetInt16
 int32  int32  -2147483648 至 2147483647  Params().GetInt32
 int64  int64   -9223372036854775808 至 9223372036854775807  Params().GetInt64
 unit  uint  0 至 18446744073709551615  Params().GetUint
 unit8  uint8  0 至 255  Params().GetUint8
 unit16  uint16  0 至 65535  Params().GetUint16
 uint32  uint32  0 至 4294967295  Params().GetUint32
 uint64  uint64  0 至 18446744073709551615  Params().GetUint64
 bool  bool  true 或 false  Params().GetBool
 alphabetical  string  小写(大写)字母  Params().Get
 file  string  小写或大写字母、数字、下划线 (_)、破折号 (-)、点 (.)  Params().Get
 path  string  可以用斜杠(路径段)分隔,但应该是路径路径的最后一部分  Params().Get

更多示例可以点这里找到


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号