Using Air to realize real-time thermal overload of Go program

Today, we will introduce an artifact - Air, which can monitor the code file of the project in real time and automatically recompile and execute after the code changes, which greatly improves the development efficiency of gin framework project.

Why do I need to load in real time?

When writing Web projects in Python, the common Flask or Django framework supports real-time loading. After you modify the project code, the program can automatically reload and execute (live reload), which is very convenient in the daily development stage.

When using the gin framework of Go language for local development and debugging, it is often necessary to press Ctrl+C frequently after changing the code to stop the program and recompile and execute it again, which is not very convenient.

Air introduction

How to realize the real-time loading function when developing based on gin framework? Such troubles are certainly not just your troubles, so I started the whole network search with the mentality that there must be ready-made wheels. Sure enough, I found a tool on Github: Air . It supports the following features:

  1. Color log output
  2. Custom build or binary commands
  3. Ignoring subdirectories is supported
  4. Support listening to new directories after startup
  5. Better build process

Installing Air

Go

This is also the most classic installation method:

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

Docker

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:

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

In order to make it easier and more convenient to type commands, you should put alias air = '~ / 'air' is added to your bashrc or In zshrc.

First enter your project directory:

cd /path/to/your_project

The simplest use is to directly execute the following commands:

# First, find `. In the current directory air.conf ` configuration file. If it cannot be found, the default configuration file will be used
air -c .air.conf

The recommended method of use is:

# 1. Create a new configuration file in the current directory air.conf
touch .air.conf

# 2. Copy ` air Conf.example ` to this file, and then modify it according to your needs

# 3. Run air with your configuration if the file name is ` air.conf `, just execute 'air'.
air

air_example.conf example

Complete air_ example. The conf sample configuration is as follows, which can be modified according to your needs.

# [Air]( https://github.com/cosmtrek/air )Configuration file in toml format

# working directory
# use. Or absolute path, please note ` TMP_ The dir directory must be under the 'root' directory
root = "."
tmp_dir = "tmp"

[build]
# Just write the shell commands you normally compile. You can also use ` make`
# Windows platform example: cmd = "go build -o tmp\main.exe."
cmd = "go build -o ./tmp/main ."
# Binary file name obtained from 'cmd' command
# Windows platform example: bin = "tmp\main.exe"
bin = "tmp/main"
# Customize the commands of the executor. You can add additional compilation IDS, such as GIN_MODE=release
# Windows platform example: full_bin = "tmp\main.exe"
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
# Listen for 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"]
# Listen for files in the following specified directory
include_dir = []
# Exclude the following files
exclude_file = []
# If the file changes too frequently, there is no need to trigger the build every time it changes. You can set the delay time to trigger the build
delay = 1000 # ms
# Stop running old binaries when a build error occurs.
stop_on_error = true
# The log file name of air, which is placed in your ` TMP_ In dir `
log = "air_errors.log"

[log]
# Show log time
time = true

[color]
# Customize the color displayed for each section. If no color is 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 demonstration

Well, now start using Air in your project and feel the convenience of real-time reload.

Keywords: Go

Added by lightningstrike on Wed, 26 Jan 2022 17:43:47 +0200