简介 Air 是一款 Go 语言热加载工具,它能实时监听文件或目录变化,并自动完成编译与重启程序操作,从而显著提升开发阶段的工作效率。
在开发过程中,要实现实时加载功能,这个困扰肯定不止你一人遇到。秉持着 “一定有现成解决方案” 的信念,我进行了全网搜索,并果真在 GitHub 上找到了一款工具 ——Air。它具备以下特性:
彩色日志输出 支持自定义构建或二进制命令 允许忽略子目录 启动后可继续监听新增目录 优化了构建过程 这款工具能够有效实现在开发中的实时加载需求。
安装 通过 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
最简单的使用方式是运行: 使用配置文件 你可以通过运行以下命令,在当前目录初始化一个默认设置的 .air.toml 配置文件:
之后,只需直接运行 air
命令,无需额外参数,它将使用 .air.toml 文件进行配置:
若要修改配置,请参考 air_example.toml 文件。
运行时参数: 你可以通过在 air
命令后添加参数来为构建后的二进制文件传递运行参数。
1 2 3 4 5 air bench air server --port 8080
你可以使用 --
参数来分隔传递给 air
命令和构建后二进制文件的参数:
1 2 3 4 5 air -- -h air -c .air.toml -- -h
Debug Goland Air 配置 首先进入你的项目目录: 1 cd /path/to/your_project
初始化默认 .air.toml 配置 golang dlv 配置 版本号为 golang 的版本 修改 .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 = ["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 配置
Debug testing 启动 air
启动 goland debug
发起请求即可开始 Debug如果不能访问,尝试启动 goland debug 后在访问
已知问题 停止 air 时 dlv 无法停止 手动解决 1 2 netstat -aon|findstr 12366 taskkill /F /IM 39560
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"]