Parameters in path

  1. func main() {
  2. app := iris.Default()
  3. // This handler will match /user/john but will not match /user/ or /user
  4. app.Get("/user/{name}", func(ctx iris.Context) {
  5. name := ctx.Params().Get("name")
  6. ctx.Writef("Hello %s", name)
  7. })
  8. // However, this one will match /user/john/ and also /user/john/send
  9. // If no other routers match /user/john, it will redirect to /user/john/
  10. app.Get("/user/{name}/{action:path}", func(ctx iris.Context) {
  11. name := ctx.Params().Get("name")
  12. action := ctx.Params().Get("action")
  13. message := name + " is " + action
  14. ctx.WriteString(message)
  15. })
  16. // For each matched request Context will hold the route definition
  17. app.Post("/user/{name:string}/{action:path}", func(ctx iris.Context) {
  18. ctx.GetCurrentRoute().Tmpl().Src == "/user/{name:string}/{action:path}" // true
  19. })
  20. app.Listen(":8080")
  21. }

Builtin available parameter types:

Param Type Go Type Validation Retrieve Helper
:string string anything (single path segment) Params().Get
:uuid string uuidv4 or v1 (single path segment) Params().Get
:int int -9223372036854775808 to 9223372036854775807 (x64) or -2147483648 to 2147483647 (x32), depends on the host arch 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), depends on the host arch 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 lowercase or uppercase letters Params().Get
:file string lowercase or uppercase letters, numbers, underscore (_), dash (-), point (.) and no spaces or other special characters that are not valid for filenames Params().Get
:path string anything, can be separated by slashes (path segments) but should be the last part of the route path Params().Get

More examples can be found at: _examples/routing.