iris入门教程 iris 获取路径中的参数

2024-02-25 开发教程 iris入门教程 匿名 8
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对应检索
stringstring字符串Params().Get
uuidstringuuidParams().Get
intint-9223372036854775808 至 9223372036854775807Params().GetInt
int8int8-128 至 127Params().GetInt8
int16int16-32768 至 32767Params().GetInt16
int32int32-2147483648 至 2147483647Params().GetInt32
int64int64-9223372036854775808 至 9223372036854775807Params().GetInt64
unituint0 至 18446744073709551615Params().GetUint
unit8uint80 至 255Params().GetUint8
unit16uint160 至 65535Params().GetUint16
uint32uint320 至 4294967295Params().GetUint32
uint64uint640 至 18446744073709551615Params().GetUint64
boolbooltrue 或 falseParams().GetBool
alphabeticalstring小写(大写)字母Params().Get
filestring小写或大写字母、数字、下划线 (_)、破折号 (-)、点 (.)Params().Get
pathstring可以用斜杠(路径段)分隔,但应该是路径路径的最后一部分Params().Get

更多示例可以点这里找到