Categories: iris 教程

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

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

4 周 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

1 月 ago