Backend Development 14 min read

Using Air for Hot Reload in Go Applications: Installation, Configuration, and Advanced Features

This article explains how to install and use the Air tool for hot‑reloading Go applications, demonstrates a simple web server example, walks through the full TOML configuration, covers advanced options, command‑line flags, and shows how to prioritize CLI arguments over config files.

Go Programming World
Go Programming World
Go Programming World
Using Air for Hot Reload in Go Applications: Installation, Configuration, and Advanced Features

During development, hot‑reload technology greatly improves experience by automatically restarting a Go application when source files change, saving time and effort. Air is a command‑line utility designed for Go that provides this capability.

Quick Start

Installation : run $ go install github.com/air-verse/air@latest . The tool is installed via the Go toolchain, so no version is specified; the latest version is used.

Project structure :

$ tree -F fly
fly/
├── README.md
└── main.go

Example code (main.go) :

package main

import (
    "fmt"
    "net/http"
    "os"
    "os/signal"
    "syscall"
)

func main() {
    signalChan := make(chan os.Signal, 1)
    signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

    server := &http.Server{Addr: ":8080"}
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "🚀 Hello Air! (PID: %d)", os.Getpid())
    })
    go func() {
        fmt.Printf("Server started at http://localhost:8080 (PID: %d)\n", os.Getpid())
        if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
            fmt.Printf("Server error: %v\n", err)
        }
    }()
    <-signalChan
    fmt.Println("Server shutting down...")
    server.Close()
}

Running air in the project root starts the server; modifying main.go triggers an automatic restart, changing the PID and response content.

Full Configuration

Air uses a TOML file ( .air.toml ) with sections such as [build] , [log] , [color] , [misc] , [screen] , and [proxy] . Key fields include:

root – project working directory.

tmp_dir – temporary files.

cmd – build command (e.g., go build -o ./tmp/main . ).

bin – path to the compiled binary.

full_bin – binary with environment variables.

args_bin – arguments passed to the binary (e.g., ["hello", "world"] ).

Various include/exclude patterns for files and directories.

Logging, color, and screen‑clear options.

Only the most frequently used options ( cmd , bin , args_bin , full_bin ) need to be customized for typical projects.

Advanced Usage

To ensure graceful shutdown logs appear, enable send_interrupt = true and set kill_delay = "1s" in the [build] section. Air will then send a Ctrl+C signal to the Go process and wait the specified delay before terminating.

Command‑line flags can override configuration values. For example, air -build.args_bin xxx runs the program with argument xxx . When both a flag and a config entry are provided, the flag takes precedence, as demonstrated by running air -build.args_bin xxx -c .air.toml .

Conclusion

Hot‑reload with Air speeds up Go development, requires only a single air command to start, and offers flexible configuration via TOML files or CLI flags. It is recommended for backend Go projects seeking rapid iteration.

backendCLIconfigurationgohot reloaddevelopment toolsair
Go Programming World
Written by

Go Programming World

Mobile version of tech blog https://jianghushinian.cn/, covering Golang, Docker, Kubernetes and beyond.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.