QueryParam extracts and parses a single query parameter from the request by key. 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 (?key=), the zero value of type T is r
(c *Context, key string, opts ...any)
| 112 | // |
| 113 | // See ParseValue for supported types and options |
| 114 | func QueryParam[T any](c *Context, key string, opts ...any) (T, error) { |
| 115 | values, ok := c.QueryParams()[key] |
| 116 | if !ok { |
| 117 | var zero T |
| 118 | return zero, ErrNonExistentKey |
| 119 | } |
| 120 | if len(values) == 0 { |
| 121 | var zero T |
| 122 | return zero, nil |
| 123 | } |
| 124 | value := values[0] |
| 125 | v, err := ParseValue[T](value, opts...) |
| 126 | if err != nil { |
| 127 | return v, NewBindingError(key, []string{value}, "query param", err) |
| 128 | } |
| 129 | return v, nil |
| 130 | } |
| 131 | |
| 132 | // QueryParamOr extracts and parses a single query parameter from the request by key. |
| 133 | // Returns defaultValue if the parameter is not found or has an empty value. |
searching dependent graphs…