Path Parameter Types
Iris provides the easiest and the most powerful routing process you have ever experienced.
Iris has its own interpreter for route’s path syntax, which can parse and evaluate complex expressions (yes, like a programming language!).
It’s fast, because it optimizes its routing based on the needs of each route. If no special regexp is needed, it registers the route with the low-level path syntax. If regexp is needed, it pre-compiles it and adds the necessary middleware(s). That means that you have zero performance cost compared to other routers or web frameworks.
Parameters
A path parameter's name should contain only alphabetical letters. Numbers or symbols like '_' are NOT allowed.
Do not confuse ctx.Params()
with ctx.Values()
.
Path parameter's values can be retrieved from
ctx.Params()
.Context's local storage that can be used to communicate between handlers and middleware(s) can be stored to
ctx.Values()
.
The built-in available parameter types can be found at the following table.
Param Type | Go Type | Validation | Retrieve Helper |
---|---|---|---|
| string | anything (single path segment) |
|
| string | uuidv4 or v1 (single path segment) |
|
| int | -9223372036854775808 to 9223372036854775807 (x64) or -2147483648 to 2147483647 (x32), depends on the host arch |
|
| int8 | -128 to 127 |
|
| int16 | -32768 to 32767 |
|
| int32 | -2147483648 to 2147483647 |
|
| int64 | -9223372036854775808 to 9223372036854775807 |
|
| uint | 0 to 18446744073709551615 (x64) or 0 to 4294967295 (x32), depends on the host arch |
|
| uint8 | 0 to 255 |
|
| uint16 | 0 to 65535 |
|
| uint32 | 0 to 4294967295 |
|
| uint64 | 0 to 18446744073709551615 |
|
| bool | "1" or "t" or "T" or "TRUE" or "true" or "True" or "0" or "f" or "F" or "FALSE" or "false" or "False" |
|
| string | lowercase or uppercase letters |
|
| string | lowercase or uppercase letters, numbers, underscore (_), dash (-), point (.) and no spaces or other special characters that are not valid for filenames |
|
| string | anything, can be separated by slashes (path segments) but should be the last part of the route path |
|
| string | uuidv4 (and v1) path parameter validation |
|
| string | email without domain validation |
|
| string | email with domain validation |
|
| string | yyyy/mm/dd format e.g. /blog/{param:date} matches /blog/2022/04/21 | `Params().SimpleDate( |
| uint or string | 0-6 positive integer or longname format ("sunday" to "monday" or "Sunday" to "Monday") |
|
Usage:
Built-in Func | Param Types |
---|---|
| :string |
| :string |
| :string |
| :string |
| :string(char length), :int, :int8, :int16, :int32, :int64, :uint, :uint8, :uint16, :uint32, :uint64 |
| :string(char length), :int, :int8, :int16, :int32, :int64, :uint, :uint8, :uint16, :uint32, :uint64 |
| :int, :int8, :int16, :int32, :int64, :uint, :uint8, :uint16, :uint32, :uint64 |
Usage:
Do It Yourself:
The RegisterFunc
can accept any function that returns a func(paramValue string) bool
. Or just a func(string) bool
. If the validation fails then it will fire 404
or whatever status code the else
keyword has.
Register your custom macro function which accepts two int arguments.
Register your custom macro function which accepts a slice of strings [...,...]
.
Example Code:
When parameter type is missing then it defaults to the
string
one, therefore{name:string}
and{name}
refers to the same exactly thing.
Last updated