gorilla/mux · error

mux: number of parameters must be multiple of 2, got %v

Error message

mux: number of parameters must be multiple of 2, got %v

What it means

Returned directly by Route.Queries (route.go:467-471) when the variadic pairs are odd. Unlike Headers, Queries checks the count itself (not via checkPairs) and returns a nil *Route on top of being a build error, so the caller must capture the return value.

Source

Thrown at route.go:469

// For example:
//
//	r := mux.NewRouter().NewRoute()
//	r.Queries("foo", "bar", "id", "{id:[0-9]+}")
//
// The above route will only match if the URL contains the defined queries
// values, e.g.: ?foo=bar&id=42.
//
// If the value is an empty string, it will match any value if the key is set.
//
// Variables can define an optional regexp pattern to be matched:
//
// - {name} matches anything until the next slash.
//
// - {name:pattern} matches the given regexp pattern.
func (r *Route) Queries(pairs ...string) *Route {
	length := len(pairs)
	if length%2 != 0 {
		r.err = fmt.Errorf(
			"mux: number of parameters must be multiple of 2, got %v", pairs)
		return nil
	}
	for i := 0; i < length; i += 2 {
		if r.err = r.addRegexpMatcher(pairs[i]+"="+pairs[i+1], regexpTypeQuery); r.err != nil {
			return r
		}
	}

	return r
}

// Schemes --------------------------------------------------------------------

// schemeMatcher matches the request against URL schemes.
type schemeMatcher []string

func (m schemeMatcher) Match(r *http.Request, match *RouteMatch) bool {

View on GitHub (pinned to db9d1d0073)

Solutions

  1. Pass an even number of args (key/value pairs).
  2. Validate len(pairs)%2 == 0 before calling.
  3. Capture and nil-check the returned *Route.

Example fix

// before
rt := r.Queries("foo", "bar", "id")
// odd -> returns nil, no matcher added

// after
rt := r.Queries("foo", "bar", "id", "")
Defensive patterns

Strategy: validation

Validate before calling

if len(qs)%2 != 0 {
    return errors.New("queries need even key/value pairs")
}
rt := r.Queries(qs...)

Type guard

func validQueriesArgs(pairs ...string) bool { return len(pairs)%2 == 0 }

Try / catch

rt := r.Queries(pairs...)
if rt == nil { return errors.New("Queries rejected the args") }

Prevention

When it happens

Trigger: r.Queries("foo", "bar", "id") (3 args); passing a half-built slice of key/value pairs whose length is odd.

Common situations: Editing a Queries call and dropping a value; building pairs dynamically with an odd length; misunderstanding the key,value,key,value contract.

Related errors


AI-assisted analysis of gorilla/mux@db9d1d0073 (2026-08-04). Data as JSON: /data/errors/bf0bd81d5741e565.json. Report an issue: GitHub.