Skip to content

Live-reload Go services in development with Air

Configure Air to watch Go source, rebuild on save, and restart the service so feedback loops feel instant. Includes Docker, Compose, and multi-binary repos.

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

  1. Generate a config.

    air init

    The default .air.toml works for a single-binary project at ./main.go. For other layouts, edit the [build] section.

  2. Tune for a cmd/<binary> layout. A cmd/api/main.go project:

    # .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 = true

    delay = 200 debounces save bursts (editors that write twice on save). kill_delay = "2s" gives your handler time to flush before SIGKILL.

  3. Pass through environment variables. air inherits the parent shell, so just export what you need:

    export DATABASE_URL=postgres://...
    export LOG_LEVEL=debug
    air

    Or use a .env file and load it via direnv or godotenv so Air picks it up.

  4. 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-cache volume keeps $GOPATH/pkg/mod warm between restarts. Compose also runs Postgres, Redis, etc. on the same network so Air-watched code can reach them by service name.

  5. Multi-binary repos. One Air per binary, separate config files:

    # In separate terminals:
    air -c .air.api.toml
    air -c .air.worker.toml

    Or wrap them in a Procfile and use overmind / goreman to start both at once:

    # Procfile
    api: air -c .air.api.toml
    worker: air -c .air.worker.toml
  6. Live-reload templates. If your service serves HTML templates, add the template extensions to include_ext so saves trigger a restart:

    include_ext = ["go", "html", "tmpl"]

    For Go services using text/template parsed at startup, a full restart is required. For services using a hot-reload library (e.g. templ watch), 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_regex to 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. Add tmp/ to .gitignore.
  • Air watches vendor/ in a fresh project. Even though the default excludes vendor, custom configs that override exclude_dir lose 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.