路径参数
路径参数
路径参数类型,出现在路由路径字符串,是一个标志位,类似于正则的命名子模式中的键,其作用间接调用验证器断言请求中的参数,断型不匹配,断言不通过则返回404。助手方法,从请求参数对象获取对应的参数值。
参数类型 | 类型 | 验证器 | 助手方法 |
---|---|---|---|
:string |
string | 任意字符串 (单一路径片段 ) | Params().Get |
:int |
int | -9223372036854775808 to 9223372036854775807 (x64) or -2147483648 to 2147483647 (x32), 取决于主机架构 | Params().GetInt |
:int8 |
int8 | -128 to 127 | Params().GetInt8 |
:int16 |
int16 | -32768 to 32767 | Params().GetInt16 |
:int32 |
int32 | -2147483648 to 2147483647 | Params().GetInt32 |
:int64 |
int64 | -9223372036854775808 to 9223372036854775807 | Params().GetInt64 |
:uint |
uint | 0 to 18446744073709551615 (x64) or 0 to 4294967295 (x32), 取决于主机架构 | Params().GetUint |
:uint8 |
uint8 | 0 to 255 | Params().GetUint8 |
:uint16 |
uint16 | 0 to 65535 | Params().GetUint16 |
:uint32 |
uint32 | 0 to 4294967295 | Params().GetUint32 |
:uint64 |
uint64 | 0 to 18446744073709551615 | Params().GetUint64 |
:bool |
bool | “1” or “t” or “T” or “TRUE” or “true” or “True” or “0” or “f” or “F” or “FALSE” or “false” or “False” | Params().GetBool |
:alphabetical |
string | 小写可大写字母转换 | Params().Get |
:file |
string | 用大/小字母, 数字, 下划线 (_), 破折号 (-), 点 (.) 且 无空格或其它对文件名中无效的特定字符 | Params().Get |
:path |
string | 任何字符串, 可被反斜杠分割 (路径片段) 路由路径的最末端除外 | Params().Get |
用法
ctx
实现上下文接口类型实例,其内持有net/http
请求与响应实例,实质是一个组合对象。
app.Get("/users/{id:uint64}", func(ctx iris.Context){
id := ctx.Params().GetUint64Default("id", 0)
// [...]
})