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

When using Python to write Web projects, the common Flask or 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.

Air Introduction

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

Install Air

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Classic installation
go get -u github.com/cosmtrek/air

# MacOS
curl -fLo air https://git.io/darwin_air

# Linux
curl -fLo air https://git.io/linux_air

# Windows
curl -fLo air.exe https://git.io/windows_air

Dcoker

1
2
3
4
5
6
7
docker run -it --rm \
    -w "<PROJECT>" \
    -e "air_wd=<PROJECT>" \
    -v $(pwd):<PROJECT> \
    -p <PORT>:<APP SERVER PORT> \
    cosmtrek/air
    -c <CONF>

Then run your project in docker as follows

1
2
3
4
5
docker run -it --rm \
    -w "/go/src/github.com/cosmtrek/hub" \
    -v $(pwd):/go/src/github.com/cosmtrek/hub \
    -p 9090:9090 \
    cosmtrek/air

Using Air

To make it easier and more convenient to hit the command, you should add alias air='~/.air' to your .bashrc or .zshrc.

First go to your project directory:

1
cd /path/to/your_project

The simplest use is to execute the following command directly:

1
2
# 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
2
3
4
5
6
7
# 1. Create a new configuration file .air.conf in the current directory
touch .air.conf

# 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

air_example.conf Example

The complete air_example.conf sample configuration is as follows and can be modified to suit your needs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# [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"

[build]
# 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"

[log]
# Show log time
time = true

[color]
# 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"

[misc]
# Delete tmp directory on exit
clean_on_exit = true

Effect Demo

go air


Reference https://www.liwenzhou.com/posts/Go/live_reload_with_air/