re-use r.URL.Query

This commit is contained in:
2026-02-20 16:54:14 +05:30
parent f1601020b1
commit 3c4d1b464c
2 changed files with 12 additions and 10 deletions

View File

@@ -19,13 +19,13 @@ type Number interface {
// NumberParam from request query string
func NumberParam[T Number](r *http.Request, key string) (T, error) {
var noop T
if !r.URL.Query().Has(key) {
q := r.URL.Query()
if !q.Has(key) {
return noop, fmt.Errorf("query param: %q is missing", key)
}
p := r.URL.Query().Get(key)
t := reflect.TypeOf(noop)
k := t.Kind()
p := q.Get(key)
k := reflect.TypeOf(noop).Kind()
// float param
if k == reflect.Float32 || k == reflect.Float64 {
n, err := strconv.ParseFloat(p, 64)