gorilla/mux · error

mux: missing name or pattern in %q

Error message

mux: missing name or pattern in %q

What it means

Returned by newRouteRegexp (regexp.go:96-97) when parsing a {name:pattern} variable in which the name or the pattern is empty. Allowed forms: {name}, {name:pattern}. Disallowed: {}, {:pattern}, {name:}. It surfaces during Path/Host/PathPrefix/Queries construction and is stored on r.err via addRegexpMatcher.

Source

Thrown at regexp.go:97

		raw := tpl[end:idxs[i]]
		end = idxs[i+1]
		tag := tpl[idxs[i]:end]

		// trim braces from tag
		param = tag[1 : len(tag)-1]

		colonIdx = strings.Index(param, ":")
		if colonIdx == -1 {
			name = param
			patt = defaultPattern
		} else {
			name = param[0:colonIdx]
			patt = param[colonIdx+1:]
		}

		// Name or pattern can't be empty.
		if name == "" || patt == "" {
			return nil, fmt.Errorf("mux: missing name or pattern in %q", tag)
		}
		// Build the regexp pattern.
		groupName := varGroupName(groupIdx)

		pattern.WriteString(regexp.QuoteMeta(raw) + "(?P<" + groupName + ">" + patt + ")")

		// Build the reverse template.
		reverse.WriteString(raw + "%s")

		// Append variable name and compiled pattern.
		varsN[groupIdx] = name
		varsR[groupIdx], err = RegexpCompileFunc("^" + patt + "$")
		if err != nil {
			return nil, fmt.Errorf("mux: error compiling regex for %q: %w", tag, err)
		}
	}
	// Add the remaining.
	raw := tpl[end:]

View on GitHub (pinned to db9d1d0073)

Solutions

  1. Provide both a name and (when using ':') a non-empty pattern: {id} or {id:[0-9]+}.
  2. Validate user-supplied path templates before registering routes.
  3. Sanitize dynamic template inputs and check route.GetError().

Example fix

// before
r.Path("/users/{:id}") // empty name

// after
r.Path("/users/{id}")
Defensive patterns

Strategy: validation

Validate before calling

var varRE = regexp.MustCompile(`^\{([^:{}]+)(?::([^{}]+))?\}$`)
func validVar(tok string) bool { return varRE.MatchString(tok) }

Type guard

func validTemplate(tpl string) bool {
    for _, tok := range extractBraces(tpl) {
        if !validVar(tok) { return false }
    }
    return true
}

Try / catch

rt := r.Path(tpl)
if err := rt.GetError(); err != nil {
    return fmt.Errorf("invalid template %q: %w", tpl, err)
}

Prevention

When it happens

Trigger: r.Path("/users/{}/"); r.Path("/users/{:id}"); r.Path("/users/{id:}"); r.Host("{:.*}.com").

Common situations: Templating bugs that emit empty placeholders; copy-paste leaving braces empty; a URL builder interpolating into path templates unsafely.

Related errors


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