Editing Go code, killing the server, recompiling, restarting — every full keystroke is a paper-cut on dev velocity. Air watches your source tree, rebuilds on save, and restarts the service in one motion. This article shows the Air configuration Stack Harbor pre-loads on Go projects so the inner loop stays under two seconds.
How to verify
Install Air, drop a config into the repo, and run:
go install github.com/air-verse/air@latest
air -v
cd /path/to/project
air init
air
air init writes a .air.toml; air watches the cwd and rebuilds. After saving a .go file you should see a fresh build log and a restart message within ~1-2 seconds for small services.
What’s happening
Air is a Go-aware file watcher and process supervisor. It uses fsnotify on Linux/macOS (kqueue, inotify) to detect file changes, runs go build against your main.go, and re-executes the new binary. While the binary runs, Air captures stdout/stderr and prints them with a prefix so you can tell rebuild output from app output. On the next save, Air sends SIGINT to the old process, waits for it to exit, then launches the new one.
Compared to running go run in a loop, Air is faster because it produces a real binary on a tmp path (default tmp/main) and re-executes it. go run compiles to a tmp file too but discards every build — Air keeps the build cache warm.
The watcher honors include/exclude lists. Files outside include_ext are ignored; directories in exclude_dir (default assets, tmp, vendor, testdata) are skipped. You add Go-only ignores by extending exclude_regex (e.g. _test\.go).
The procedure
-
Generate a config.
air initThe default
.air.tomlworks for a single-binary project at./main.go. For other layouts, edit the[build]section. -
Tune for a
cmd/<binary>layout. Acmd/api/main.goproject:# .air.toml root = "." tmp_dir = "tmp" [build] cmd = "go build -gcflags='all=-N -l' -o ./tmp/api ./cmd/api" bin = "./tmp/api" full_bin = "./tmp/api --config=local.yaml" include_ext = ["go", "tpl", "tmpl", "html", "sql"] exclude_dir = ["assets", "tmp", "vendor", "testdata", "node_modules", "dist"] exclude_regex = ["_test\\.go"] delay = 200 stop_on_error = true send_interrupt = true kill_delay = "2s" [log] time = true main_only = false [color] main = "magenta" watcher = "cyan" build = "yellow" runner = "green" [misc] clean_on_exit = truedelay = 200debounces save bursts (editors that write twice on save).kill_delay = "2s"gives your handler time to flush before SIGKILL. -
Pass through environment variables.
airinherits the parent shell, so justexportwhat you need:export DATABASE_URL=postgres://... export LOG_LEVEL=debug airOr use a
.envfile and load it viadirenvorgodotenvso Air picks it up. -
Run Air inside Docker Compose. A dev-only compose service that mounts the source tree:
# docker-compose.dev.yml services: api: image: golang:1.22.5 working_dir: /app volumes: - .:/app - go-cache:/go command: ["sh", "-c", "go install github.com/air-verse/air@latest && air"] ports: - "8080:8080" env_file: .env volumes: go-cache: {}The
go-cachevolume keeps$GOPATH/pkg/modwarm between restarts. Compose also runs Postgres, Redis, etc. on the same network so Air-watched code can reach them by service name. -
Multi-binary repos. One Air per binary, separate config files:
# In separate terminals: air -c .air.api.toml air -c .air.worker.tomlOr wrap them in a Procfile and use
overmind/goremanto start both at once:# Procfile api: air -c .air.api.toml worker: air -c .air.worker.toml -
Live-reload templates. If your service serves HTML templates, add the template extensions to
include_extso saves trigger a restart:include_ext = ["go", "html", "tmpl"]For Go services using
text/templateparsed at startup, a full restart is required. For services using a hot-reload library (e.g.templwatch), Air can run alongside the template watcher.
Common pitfalls
- Editors that write atomic-via-rename. Some editors (vim with backup, JetBrains) write to a temp file then rename. Air sees the rename but may miss the write — set
exclude_regexto include the editor’s backup extensions. - A long-running goroutine that ignores SIGINT. If your service does not honor
signal.NotifyContext, the kill takes 2 seconds (kill_delay) and feels sluggish. Implement graceful shutdown. - Forgetting
-gcflags='all=-N -l'. Without it, Delve cannot attach to the dev binary. Air does not set it by default. - The default
tmp/directory is committed by accident. Addtmp/to.gitignore. - Air watches
vendor/in a fresh project. Even though the default excludes vendor, custom configs that overrideexclude_dirlose the default. Re-add it manually.
Stack Harbor preloads Air in every Go project we hand to a development team, with the config tuned to their layout. The dev loop and the production operating model are different but should still produce the same binary semantics — see how that fits into our managed operations practice.