Summary: this tutorial will teach you how to use the air utility to set up a live reload for Go programs to boost your productivity and streamline your development workflow
Step 1. Open your terminal and create a new project directory:
mkdir airdemo
Step 2. Navigate to the project directory:
cd airdemo
Step 3. Create main.go file with the following code:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Code language: JavaScript (javascript)
Step 4. Run the go init
command:
go mod init airdemo
This will create a go.mod
file that stores the Go versions and the project’s dependencies.
Step 5. Install the air utility by running this command from the terminal:
go install github.com/air-verse/air@latest
Step 6. Create the air configuration file:
air init
This command will create the .air.toml
file that stores the air configuration parameters.
A sample .air.toml file on Windows
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "tmp\\main.exe"
cmd = "go build -o ./tmp/main.exe ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
time = false
[misc]
clean_on_exit = false
[proxy]
app_port = 0
enabled = false
proxy_port = 0
[screen]
clear_on_rebuild = false
keep_scroll = true
Code language: JavaScript (javascript)
Step 7. Run the air command on your terminal:
air
The output will look like this:
__ _ ___
/ /\ | | | |_)
/_/--\ |_| |_| \_ v1.52.3, built with Go go1.23.0
watching .
!exclude tmp
building...
running...
Hello, World!
Process Exit with Code 0
Code language: PHP (php)
Step 8. Change the code in the main.go
, the Air will automatically detect the change and rebuild the program:
package main
import "fmt"
func main() {
fmt.Println("Hi there!")
}
Code language: JavaScript (javascript)
Air will produce the following output:
main.go has changed
building...
running...
Hi there!
Process Exit with Code 0
Code language: JavaScript (javascript)