# Replace go-bindata With go:embed in a Go Web Server

Yes. An agent deletes the generated go-bindata file, embeds the same assets with go:embed, and compares every served response before and after the change.

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

## What the agent does

A project on go-bindata carries three things `embed` makes unnecessary: a generator step, a tools file that pins the generator, and a generated source file with every asset inside it. The agent finds all three. In a scheduler like ajvb/kala, the `Makefile` target `gen` runs `go-bindata-assetfs -pkg api -o api/webui_bindata.go webui/...`, `tools.go` imports `github.com/go-bindata/go-bindata` and `github.com/elazarl/go-bindata-assetfs` behind a `tools` build tag, and `api/webui_bindata.go` is a 2,069,615-byte file holding 25 assets. One line consumes it: `api/api.go` mounts `http.FileServer(assetFS())` under `/webui/`.

Before deleting anything, the agent checks whether the generated file is stale, since a stale file means the port would change what users are served. A temporary test walks `webui/` and compares each file with `Asset(name)`. Here all 25 match. It also records the handler's answers for a list of paths (the index, a script, a stylesheet directory, a redirect, a missing file) with status, `Content-Type`, length, a hash of the body and the `Last-Modified` header.

The obvious port fails twice, and the agent expects both. Putting `//go:embed ../webui` in the `api` package is rejected with `pattern ../webui: invalid pattern syntax`, because a pattern cannot leave its own package directory. So the directive goes in a new file inside `webui/` that declares `package webui` and exports an `embed.FS`, and `api.go` serves `http.FS(webui.Files)`. Then the build stops with `go:embed requires go1.16 or later (-lang was set to go1.13; check go.mod)`, and the `go` line in `go.mod` moves from 1.13 to 1.16. The pattern lists `*.html *.png *.ico *.webmanifest css js` instead of `*`, which would have embedded and served the new Go file as well.

After `go mod tidy` both go-bindata modules leave `go.mod`. The agent reruns the recorded requests and diffs them: every status, type, length and body hash is the same, and one header differs. The old handler sent a `Last-Modified` equal to the time the process started; an embedded file has no modification time, so the new one sends none. It builds both binaries and reports their sizes, which grow slightly because the generated file stored its data gzip-compressed and `embed` does not. The repository's CircleCI job uses the `cimg/go:1.17.13` image, so the agent installs that Go release for the test run and does not rely on a newer one.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Replace go-bindata (and go-bindata-assetfs if present) with the standard library embed package in this Go module. Find the generator command (Makefile, go:generate, scripts), the tools.go entries, the generated file and every call to Asset, AssetDir, AssetNames, MustAsset or assetFS. Before changing anything, write a temporary test that compares every file in the asset directory with what the generated file returns, and records status, Content-Type, body length, a body hash and Last-Modified for a set of paths through the real handler: the index, a file in a subdirectory, a directory, a path with no trailing slash and a missing file. Use the Go version the CI config pins for running tests. Then add the go:embed directive in a package whose directory contains the assets (a pattern cannot use ..), list patterns so no Go source file is embedded, raise the go directive in go.mod only if the build requires it and check every CI and Docker image is at least that version. Serve through http.FS, delete the generated file, the tools imports and the generator targets, and run go mod tidy. Rerun the recorded requests and show the diff, build the binary before and after and report both sizes, run go vet and the tests, then remove the temporary test. 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 deletes `api/webui_bindata.go` and `tools.go`, adds a short `webui/embed.go`, changes one line and one import in `api/api.go`, removes the `gen` and `tools` targets from the `Makefile`, and edits `go.mod` and `go.sum`. Most of the diff is the removal of the generated file. The description holds the drift check, the two build errors and how each was resolved, the response comparison and the binary sizes.

The reviewer weighs the one behavioural difference, the missing `Last-Modified` header. Without it a browser has no validator for these files and cannot make a conditional request. If that matters for the web interface, the description says what to add: a wrapper that sets an `ETag` from the build's version string. They also check the Go floor. The directive rises to 1.16, the lint workflow pins Go 1.17 and CircleCI uses 1.17.13, so no CI file needs to move in this repository.

## Run it on every issue labelled modernize

Add an automation on the Project with a `github` trigger on `issues.labeled`, put your label in its `labels` filter, and save the prompt above. When a maintainer labels the issue that tracks the generator's removal, a run starts in the Project's repository with the issue appended after the prompt. Only an owner, member or collaborator of the repository fires the trigger, so a label added by a bot starts nothing.

To convert several services in one pass, post this body to `POST /o/{org}/sessions` for each repository with a `request_id` that is a UUID v5 of the repository name. Each run answers `202` at once; follow one on `GET /o/{org}/sessions/{id}/events` or read its result when it finishes.

## Why run it on Rush?

The claim a reviewer needs is that users get the same files, and that is a comparison of responses, not a reading of the diff. Doing it means building the old and new server, running requests through the real router and keeping both sets of answers. The agent does that in a sandbox that has nothing else on it, and the answers, the commands and the two build errors stay in the run's transcript.

It also pins down an environment detail that is easy to miss on a laptop: which Go release the tests are run with. The run installs the version the CI config names, so a pass or failure in the pull request means the same thing it will mean in CI.

## Frequently asked questions

### Why does go:embed reject a path with two dots?

Patterns are resolved inside the directory of the package that contains the directive and may not climb out of it. When assets live beside the package and not under it, put a small Go file in the asset directory itself, export the `embed.FS` from there and import that package where the handler is built.

### Does embed keep file modification times?

No. Files in an `embed.FS` report a zero modification time, so `http.FileServer` sends no `Last-Modified` header for them. The comparison in the run shows this as the only difference in the responses. Add an `ETag` yourself if clients should revalidate.

### Will the binary get bigger?

A little, when the generated file was compressed. go-bindata stores assets gzip-compressed by default and unpacks them at run time, while `embed` stores the bytes as they are. The agent builds both and puts the two sizes in the pull request.

### Which Go version does embed need?

Go 1.16 or later, and the `go` line in `go.mod` has to say so; otherwise the compiler refuses the directive even on a new toolchain. The agent raises the line only as far as needed and checks each CI and builder image against it.

## More jobs like this: Upgrade dependencies weekly

- [Replace mitchellh/go-homedir With os.UserHomeDir in a Go CLI](https://getrush.ai/run/migrate-go-homedir-to-os-userhomedir/go-homedir/)
- [Replace hashicorp/go-multierror With errors.Join in a Go Project](https://getrush.ai/run/migrate-go-multierror-to-errors-join/go-multierror/)
- [Migrate From go-redis v8 to v9 (github.com/redis/go-redis/v9)](https://getrush.ai/run/migrate-go-redis-v8-to-v9/go-redis/)
- [Replace mattn/go-sqlite3 With modernc.org/sqlite for a cgo-Free Go Build](https://getrush.ai/run/migrate-go-sqlite3-to-modernc-sqlite/go-sqlite3/)
- [Migrate From gopkg.in/yaml.v2 to yaml.v3 in a Go Project](https://getrush.ai/run/migrate-go-yaml-v2-to-v3/go-yaml/)
- [Migrate From github.com/golang/protobuf to google.golang.org/protobuf](https://getrush.ai/run/migrate-golang-protobuf-to-google-protobuf/golang-protobuf/)

## 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/)
