Today we want to introduce a magic tool - Air can listen to the project’s code files in real time, and automatically recompile and execute the code after changes, greatly improving the development efficiency of
gin framework projects.
Why do I need real-time loading
Python to write
Web projects, the common
Django frameworks support live-loading, and after you modify the project code, the program can automatically reload and execute (live-reload), which is very convenient in the daily development phase.
When using the
gin framework in the
Go language to do local development debugging, you often need to frequently press
Ctrl+C after changing the code to stop the program and recompile and re-execute, which is not very convenient.
How can I implement the live loading feature when developing based on the
gin framework? This kind of trouble is not just for you, so I started searching the web with the idea that there must be a ready-made tool. As expected, I found a tool on
Github: Air. It supports the following features
- Color log output
- Custom build or binary commands
- Support for ignoring subdirectories
- Support for listening to new directories after startup
- Better build process
# Classic installation
go get -u github.com/cosmtrek/air
curl -fLo air https://git.io/darwin_air
curl -fLo air https://git.io/linux_air
curl -fLo air.exe https://git.io/windows_air
docker run -it --rm \
-w "<PROJECT>" \
-e "air_wd=<PROJECT>" \
-v $(pwd):<PROJECT> \
-p <PORT>:<APP SERVER PORT> \
Then run your project in docker as follows
docker run -it --rm \
-w "/go/src/github.com/cosmtrek/hub" \
-v $(pwd):/go/src/github.com/cosmtrek/hub \
-p 9090:9090 \
To make it easier and more convenient to hit the command, you should add
alias air='~/.air' to your
First go to your project directory:
The simplest use is to execute the following command directly:
# First, look for the `.air.conf` configuration file in the current directory, and if you can't find it, use the default
air -c .air.conf
The recommended method of use is:
# 1. Create a new configuration file .air.conf in the current directory
# 2. Copy the contents of `air.conf.example` to this file, then modify it as you see fit
# 3. Run air with your configuration, and if the file name is `.air.conf`, just execute `air`.
air_example.conf sample configuration is as follows and can be modified to suit your needs.
# [Air](https://github.com/cosmtrek/air) Configuration file in TOML format
# work dir
# Use . or absolute path, please note that the `tmp_dir` directory must be in the `root` directory
root = "."
tmp_dir = "tmp"
# Just write the shell commands you normally use to compile. You can also use `make`
# Windows platform example: cmd = "go build -o tmp\main.exe ."
cmd = "go build -o ./tmp/main ."
# The name of the binary file obtained by the `cmd` command
# Example for Windows platform: bin = "tmp\main.exe"
bin = "tmp/main"
# Customize the command to execute the program by adding an additional compile flag such as GIN_MODE=release
# Example for Windows platform: full_bin = "tmp\main.exe"
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
# Listens to files with the following file extensions.
include_ext = ["go", "tpl", "tmpl", "html"]
# Ignore these file extensions or directories
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
# Listens to files in the following specified directories
include_dir = 
# Exclude the following documents
exclude_file = 
# If the file changes too frequently, it is not necessary to trigger a build on every change. You can set the delay time for triggering the build
delay = 1000 # ms
# Stop running old binaries when a build error occurs.
stop_on_error = true
# The name of the log file that is placed in your `tmp_dir`
log = "air_errors.log"
# Show log time
time = true
# Customize the colors displayed for each section. If the color is not found, use the original application log.
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"
# Delete tmp directory on exit
clean_on_exit = true