Skip to content

Migrating off gorilla/mux to net/http or chi

gorilla/mux was archived in 2022 and revived in 2023. Decide whether to stay, move to stdlib net/http (Go 1.22+), or migrate to chi — with a checklist for each path.

gorilla/mux was the default Go router for a decade. The Gorilla project was archived in December 2022, briefly leaving every service depending on it in awkward territory, and was revived in late 2023 under a new community maintainership. Meanwhile Go 1.22 added pattern-matching (path parameters, method-bound routes) to the stdlib net/http ServeMux. This article is the operator’s decision tree: stay, move to stdlib, or migrate to chi.

How to verify

Check whether your project still uses gorilla/mux and what it actually needs from it:

go list -m all | grep gorilla/mux
grep -rn 'github.com/gorilla/mux' .
grep -rn 'mux.Vars\|mux.Router' .
grep -rn 'Subrouter\|HandleFunc\|UseEncodedPath\|StrictSlash' .

The features you exercise determine the migration path. A project that only uses mux.NewRouter().HandleFunc(...) and mux.Vars(r) can move to stdlib in 1.22. A project that uses subrouters, host matchers, or StrictSlash keeps more semantics by moving to chi.

What’s happening

Three router families live in modern Go:

  • stdlib net/http (Go 1.22+): supports method-prefixed patterns (GET /users/{id}), path wildcards (/static/{path...}), and per-host routing. No middleware concept, no subrouters, no regex on params. Zero dependencies, maintained by the Go core team.
  • chi (github.com/go-chi/chi/v5): standalone router compatible with http.Handler. Provides middleware chains, subrouters via Route/Mount, URL param helpers, and routes-with-context propagation. Actively maintained, single dependency, idiomatic Go style.
  • gorilla/mux: revived under gorilla.github.io/mux. Same API as before. Supports host matching, header matching, scheme matching, full regex on path params, and StrictSlash. Maintenance is community-driven.

The community signal in 2024-26 is that chi has more momentum than the revived gorilla/mux. New code we audit overwhelmingly uses chi or stdlib; gorilla appears mostly in legacy code.

Decision

Pick by which features you actually use:

You needUse
Only method + path patternsstdlib (Go 1.22+)
Middleware chains, subrouters, ergonomic paramschi
Host matching, header matching, regex paths, StrictSlashgorilla/mux (revived)

A net-new service has no good reason to start with gorilla — start with stdlib and adopt chi when you need middleware composition.

The procedure

  1. Migrating mux.Vars(r) to Go 1.22 stdlib.

    // gorilla/mux
    r := mux.NewRouter()
    r.HandleFunc("/users/{id}", getUser).Methods("GET")
    
    func getUser(w http.ResponseWriter, r *http.Request) {
        id := mux.Vars(r)["id"]
        // ...
    }

    Becomes:

    // stdlib net/http (Go 1.22+)
    mux := http.NewServeMux()
    mux.HandleFunc("GET /users/{id}", getUser)
    
    func getUser(w http.ResponseWriter, r *http.Request) {
        id := r.PathValue("id")
    }

    r.PathValue replaces mux.Vars. Method binding is now part of the pattern.

  2. Migrating to chi. A drop-in replacement with familiar shape:

    import "github.com/go-chi/chi/v5"
    
    r := chi.NewRouter()
    r.Use(middleware.Logger)
    r.Use(middleware.Recoverer)
    r.Route("/api", func(r chi.Router) {
        r.Get("/users/{id}", getUser)
        r.Post("/users", createUser)
    })
    
    func getUser(w http.ResponseWriter, r *http.Request) {
        id := chi.URLParam(r, "id")
    }

    chi.URLParam replaces mux.Vars. Subrouters with Route/Mount replace Subrouter.

  3. Migrating middleware. Gorilla’s r.Use(...) middlewares are pure http.Handler decorators, so they port directly:

    func auth(next http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            if !checkToken(r) { http.Error(w, "unauthorized", 401); return }
            next.ServeHTTP(w, r)
        })
    }

    Same function works with chi.Router.Use(auth), with stdlib via a single wrapping function, and with gorilla r.Use(auth).

  4. Handling features stdlib does not have. If you depended on:

    • Host matching: stdlib supports Host() patterns since 1.22 (mux.Handle("api.example.com/", ...)).
    • Header matching: not in stdlib or chi. Roll into middleware.
    • Regex on path params: not in stdlib. Chi only supports {name:[0-9]+}. Gorilla retains full regex.
    • StrictSlash: stdlib has trailing-slash normalization built into its pattern matcher; chi has middleware.StripSlashes and middleware.RedirectSlashes. Behavior is not identical to gorilla.
  5. Run the migration as a vet-staged change.

    go test ./...        # baseline
    # change one handler group at a time
    go test ./...
    # http integration tests against the change
    go run ./cmd/api & sleep 1; curl -i localhost:8080/users/42; kill %1

    Avoid a big-bang migration; routers touch every endpoint.

  6. Keep net/http.Handler everywhere. All three options compose around http.Handler and http.HandlerFunc. Code that takes/returns those is portable across routers — write your business handlers against http.Handler, not against a router-specific type.

Common pitfalls

  • Gorilla’s mux.Router is an http.Handler but not interchangeable with chi’s chi.Router. Adapter code that depends on the concrete type breaks. Refactor to http.Handler.
  • r.PathValue("id") returns empty string for missing keys, not error. Add explicit validation.
  • Go 1.22 stdlib patterns have a precedence model. Specific patterns win over general ones (/users/me over /users/{id}). Test pairs that overlap.
  • chi’s subrouter does not inherit middleware unless mounted explicitly. Read the docs on Route vs Mount vs Group — same surface, different propagation rules.
  • Regex matching is the silent migration killer. Audit r.HandleFunc("/{slug:[a-z0-9-]+}", ...) first — neither stdlib nor chi supports gorilla’s full regex.

Stack Harbor migrates legacy gorilla/mux services to chi (or stdlib for small services) as part of long-term operations engagements — see golang-chi-router-pattern for the target pattern. The migration fits inside our managed operations practice when a dependency goes out of active maintenance.