iris 获取路径中的参数

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

更多示例可以点这里找到

作者:terry,如若转载,请注明出处:https://www.web176.com/iris/21995.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年5月19日
下一篇 2023年5月19日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注