简介

Air 是一款 Go 语言热加载工具,它能实时监听文件或目录变化,并自动完成编译与重启程序操作,从而显著提升开发阶段的工作效率。

在开发过程中,要实现实时加载功能,这个困扰肯定不止你一人遇到。秉持着 “一定有现成解决方案” 的信念,我进行了全网搜索,并果真在 GitHub 上找到了一款工具 ——Air。它具备以下特性:

  1. 彩色日志输出
  2. 支持自定义构建或二进制命令
  3. 允许忽略子目录
  4. 启动后可继续监听新增目录
  5. 优化了构建过程

这款工具能够有效实现在开发中的实时加载需求。

安装

通过 go install (推荐)

1
2
3
go install github.com/cosmtrek/air@latest

air -v

通过 install.sh

1
2
3
4
5
6
7
# binary will be $(go env GOPATH)/bin/air
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin

# or install it into ./bin/
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s

air -v

快速使用

为了减少输入,你可以在 .bashrc 或 .zshrc 文件中添加别名 air=’~/.air’。

首先进入你的项目目录:

1
cd /path/to/your_project
  1. 最简单的使用方式是运行:
1
2
# 直接启动当前目录下的程序
air
  1. 使用配置文件
1
2
# 首先在当前目录查找 .air.toml,如果没有找到,则使用默认配置
air -c .air.toml

你可以通过运行以下命令,在当前目录初始化一个默认设置的 .air.toml 配置文件:

1
air init

之后,只需直接运行 air 命令,无需额外参数,它将使用 .air.toml 文件进行配置:

1
air

若要修改配置,请参考 air_example.toml 文件。

运行时参数:
你可以通过在 air 命令后添加参数来为构建后的二进制文件传递运行参数。

1
2
3
4
5
# 将运行 ./tmp/main bench
air bench

# 将运行 ./tmp/main server --port 8080
air server --port 8080

你可以使用 -- 参数来分隔传递给 air 命令和构建后二进制文件的参数:

1
2
3
4
5
# 将运行 ./tmp/main -h
air -- -h

# 将使用自定义配置运行 air,并将 -h 参数传递给构建后的二进制文件
air -c .air.toml -- -h

Debug

Goland

Air 配置

  1. 首先进入你的项目目录:
1
cd /path/to/your_project
  1. 初始化默认 .air.toml 配置
1
air init
  1. golang dlv 配置
    版本号为 golang 的版本
1
go install github.com/go-delve/delve/cmd/[email protected]
  1. 修改 .air.toml 配置
    主要关注一下几个配置
1
2
3
bin = "tmp/main.exe"
cmd = "go build -o ./tmp/main.exe {{项目路径}}"
full_bin = "{{dlv路径}} --listen=:{{监听端口}}--headless=true --api-version=2 --accept-multiclient exec tmp/main.exe"

完整配置示例如下:

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
47
root = "." # 监控目录
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "tmp/main.exe"
cmd = "powershell.exe go build -gcflags 'all=-N -l' -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 = "dlv --listen=:12366 --headless=true --api-version=2 --continue --accept-multiclient exec tmp/main.exe"
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
#post_cmd = ["powershell.exe Stop-Process -Id (Get-NetTCPConnection -LocalPort 12388).OwningProcess -Force"]
post_cmd = ["echo killing the dlv process.", "powershell.exe Stop-Process -Id (Get-NetTCPConnection -LocalPort 12366).OwningProcess -Force", "echo kille success"]
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 = true
time = true

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
keep_scroll = true

Goland 配置

https://minio.kl.do/picture/images/typora/a1fa2354cad9db7cc1a90f8ec081253a.png

https://minio.kl.do/picture/images/typora/80756269a3e893ab66a19083a75d285e.png

https://minio.kl.do/picture/images/typora/8503c5b436522935f61a364bcbb468f7.png

Debug testing

  1. 启动 air
    1
    air

https://minio.kl.do/picture/images/typora/21fc5ab256d078e256fdcec9bc95da65.png

  1. 启动 goland debug

https://minio.kl.do/picture/images/typora/e0d7d3e48c38744acde5b45e37899ae1.png

  1. 发起请求即可开始 Debug
    如果不能访问,尝试启动 goland debug 后在访问

https://minio.kl.do/picture/images/typora/7758e122fe33a0193d7440fe719a19fc.png

已知问题

停止 air 时 dlv 无法停止

  1. 手动解决
1
2
netstat -aon|findstr 12366
taskkill /F /IM 39560
  1. windows air 解决方案
    程序结束后将 dlv 端口程序强制停止
1
2
3
#post_cmd = ["powershell.exe Stop-Process -Id (Get-NetTCPConnection -LocalPort 12366).OwningProcess -Force"]

post_cmd = ["echo killing the dlv process.", "powershell.exe Stop-Process -Id (Get-NetTCPConnection -LocalPort 12366).OwningProcess -Force", "echo kille success"]