PathParam extracts and parses a path parameter from the context by name. It returns the typed value and an error if binding fails. Returns ErrNonExistentKey if parameter not found. Empty String Handling: If the parameter exists but has an empty value, the zero value of type T is returned with no
(c *Context, paramName string, opts ...any)
| 57 | // |
| 58 | // See ParseValue for supported types and options |
| 59 | func PathParam[T any](c *Context, paramName string, opts ...any) (T, error) { |
| 60 | for _, pv := range c.PathValues() { |
| 61 | if pv.Name == paramName { |
| 62 | v, err := ParseValue[T](pv.Value, opts...) |
| 63 | if err != nil { |
| 64 | return v, NewBindingError(paramName, []string{pv.Value}, "path value", err) |
| 65 | } |
| 66 | return v, nil |
| 67 | } |
| 68 | } |
| 69 | var zero T |
| 70 | return zero, ErrNonExistentKey |
| 71 | } |
| 72 | |
| 73 | // PathParamOr extracts and parses a path parameter from the context by name. |
| 74 | // Returns defaultValue if the parameter is not found or has an empty value. |
searching dependent graphs…