# Replace gorilla/mux With the Go Standard Library Router

Yes. An agent ports gorilla/mux routes to net/http patterns, raises the go directive they depend on, and proves which responses changed.

[Run this on Rush](https://cloud.getrush.ai/run/migrate-gorilla-mux-to-net-http/gorilla-mux/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

Since Go 1.22 the standard `http.ServeMux` matches methods and path wildcards, which covers what many small APIs use gorilla/mux for. The agent first lists what this router is asked to do. In an API like TomFern/go-mux-api everything is in `app.go`: five routes in `initializeRoutes`, each a `HandleFunc(...).Methods(...)`, three of them on `/product/{id:[0-9]+}`; three handlers that read `mux.Vars(r)["id"]`; and an exported `Router *mux.Router` field that `main_test.go` drives with `a.Router.ServeHTTP`. `go.mod` says `go 1.16` and requires `github.com/gorilla/mux v1.8.0` and `github.com/lib/pq v1.10.0`. There is no middleware, subrouter, host matching or named route, so nothing needs a replacement the standard library lacks.

The tests need PostgreSQL, so the agent installs it in the sandbox and creates the role the suite expects. `env-test` leaves the password empty, and a default Debian install refuses that over TCP with `pq: password authentication failed for user "postgres"`, so it sets a password and exports it for the run. The six tests in `main_test.go` then pass on the unchanged code. Before porting, the agent adds a probe test that records status, `Allow` header and body for requests the suite never makes: a non-numeric id, a negative id, `HEAD`, a wrong method, a trailing slash.

The port itself is short: `"GET /products"`, `"POST /product"` and `"GET /product/{id}"` style patterns, `r.PathValue("id")` in place of `mux.Vars`, and `*http.ServeMux` for the field. What the agent finds next is the reason to run it instead of reading it. With `go 1.16` still in `go.mod` the code compiles and all six tests fail with 404. The method-and-wildcard patterns are switched on by the module's `go` line, and below 1.22 the mux treats `"GET /products"` as a literal path. Raising the line to 1.22 makes the six pass. The same number is pinned as `sem-version go 1.16` in four places in `.semaphore/semaphore.yml`, so the agent changes those in the same pull request.

Then it runs the probe again and reads the differences. The regular expression is gone from the pattern, so `GET /product/abc` reaches the handler and answers 400 `Invalid product ID` where gorilla answered a plain 404. `GET /product/-1` now runs a query and returns the JSON 404, and `DELETE /product/-1` would report success. `HEAD /products` was 405 and is now 200, because a `GET` pattern also serves `HEAD`. A wrong method is still 405 but now carries `Allow: DELETE, GET, HEAD, PUT` and a body. The agent restores the old 404 for ids that are not digits with a check at the top of each handler, and lists the rest for the reviewer.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Replace gorilla/mux with the standard library http.ServeMux in this Go API. Read go.mod, every file that imports github.com/gorilla/mux, the tests and the CI configuration. List each mux feature in use: route patterns and their regular expressions, Methods, Vars, middleware, subrouters, named routes, anything else. If a feature has no ServeMux equivalent, stop and report it. There is no Docker here: install PostgreSQL with apt, start it, set a password for the test role and export the variables the tests read, then run go test ./... on the unchanged code and keep the output. Add a test that records status code, Allow header and body for requests the suite does not cover: a non-numeric id, a negative id, an id that overflows int, HEAD, an unsupported method, a trailing slash, an unknown path. Run it before the change. Port the routes to method and wildcard patterns and r.PathValue. Run the tests with the go directive unchanged and report what happens, then raise the directive to the lowest version that makes them pass and update every CI or image file that pins an older Go. Run the probe again, show every difference, and keep the old response where a path used to be rejected by a route regular expression. Run go mod tidy and go vet. Open a pull request with every command and its output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that changes `app.go`, `go.mod`, `go.sum` and the Semaphore pipeline, and adds the probe test. After `go mod tidy` the module requires only `github.com/lib/pq`. The description has the feature list, the six tests passing before, failing with the old `go` line and passing with the new one, and the probe output side by side with each difference marked as kept, restored or new.

The reviewer decides on the differences, since they are visible to clients. `HEAD` answering 200 and 405 responses gaining an `Allow` header are what the standard router does and are usually welcome. Ids such as `-1` reaching the database are not, which is why the digit check is in the diff. One thing stays as it was and is noted: `Run` ignores its `addr` argument and always listens on `:8010`.

## Run it on every Linear issue labelled tech-debt

Dropping a router dependency is the kind of issue that waits in a backlog. Connect Linear under Settings → Connections, then add an automation on the Project with a `linear` trigger on `linear.issue.labeled`, the label your team uses for this work, and a saved prompt like the one above. The issue is added after the prompt as event text. The saved prompt stays the only instruction, so write it to do the migration and to stop when a route uses something `http.ServeMux` cannot express.

The trigger fires when a person in your Rush org adds the label, never when an app integration does. Each service gets its own run and its own pull request, and the automation's max runs per hour decides how many start before the rest wait.

## Why run it on Rush?

The two findings that matter here cannot be seen in the diff. That every route returns 404 until the `go` directive moves, and that four requests change their answer afterwards, come from running the tests and the probe against a real database. A run on Rush has its own sandbox for that: PostgreSQL installed for this job, the commands and their output kept, and nothing shared with another task.

It also keeps the change honest about scope. The probe output before and after is in the pull request, so a reviewer approves a list of behaviour changes instead of trusting that a router swap has none.

## Frequently asked questions

### Which gorilla/mux features have no standard library equivalent?

Regular expressions inside path variables, header and query matchers, subrouters with their own middleware, and named routes with URL building. Wildcards and methods are covered. The agent lists what your code uses first and stops if the list includes something that would need hand-written code to replace.

### Why did every test return 404 after the port?

Because `go.mod` still said `go 1.16`. Patterns such as `GET /product/{id}` are enabled by the module's `go` line, and below 1.22 the mux reads them as literal paths. The code compiles either way, which is why the failure only shows when the tests run.

### Is gorilla/mux unmaintained?

No. The repository is public and not archived, and its latest release is v1.8.1 from October 2023. This migration is about having one dependency fewer now that the standard library does the same job, not about an abandoned package.

### Do I have to change my tests?

Not in this repository. The suite calls `a.Router.ServeHTTP`, which `*http.ServeMux` also has, so the six tests run unchanged. Tests that build URLs from named routes or inspect `mux.Vars` directly would need edits, and the agent reports those before it starts.

## More gorilla/mux jobs

- [Write Unit Tests for a Gorilla Mux REST API](https://getrush.ai/run/write-unit-tests/gorilla-mux/)

## More jobs like this: Turn a Linear label into a PR

- [Replace Deprecated io/ioutil With os and io in a Go Program](https://getrush.ai/run/migrate-ioutil-to-os-and-io/ioutil/)
- [Migrate From lib/pq to pgx in a Go API](https://getrush.ai/run/migrate-lib-pq-to-pgx/lib-pq/)
- [Migrate From Logrus to log/slog in a Go CLI](https://getrush.ai/run/migrate-logrus-to-slog/logrus/)
- [Migrate mkdirp to fs.mkdir with recursive: true](https://getrush.ai/run/migrate-mkdirp-to-fs-mkdir/mkdirp/)
- [Replace Moment.js With Day.js in an Express API](https://getrush.ai/run/migrate-moment-to-dayjs/moment/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
