path-parameters

command
v14.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Path parameters: typed macros in the route pattern, typed accessors in the handler.

A macro such as {id:int} validates and converts the segment BEFORE any handler runs, so a handler that runs can trust its input. A request that does not match never reaches your code: the router answers 404 by default, or the status code given by a trailing "else N".

ctx.Params() has typed accessors with fixed failure defaults. ctx.BindParam is the helper form: it converts the parameter and, when that cannot be done, answers the client itself (404 when the parameter is missing, 400 when it has another type) and reports false, so the handler's only branch is a one-line return. With a typed macro such as {id:int} the router has already rejected a non-integer, so that branch fires only for a handler reused on a route that does not carry the parameter.

Learn: register typed path parameters and let the router reject bad values with 404, or with the status of a trailing "else N", before your handler runs. Before: ../routing-basics

Run:

go run main.go

Try:

curl http://localhost:8080/greet/iris                                   # 200, {"message":"hello, iris"}
curl http://localhost:8080/orders/42                                    # 200, {"order":42,"status":"pending"}
curl -i http://localhost:8080/orders/abc                                # 404, "abc" is not an int
curl http://localhost:8080/shelves/9f86d081-8292-4c4c-812f-0a1b2c3d4e5f  # 200, {"shelf":"9f86d081-..."}
curl -i http://localhost:8080/page/0                                    # 400, from "else 400"
curl http://localhost:8080/page/7                                       # 200, {"page":7}
curl http://localhost:8080/carts/3                                      # 200, {"cart":3}

Next: ../groups

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL