Go Live Reload Tool: air
https://github.com/cosmtrek/air
在 Go 项目的开发中,我们经常会有下面这种重复的行为:
- 在 IDE 编写部分代码;
- 在命令行输入
go run main.go
启动进程; - 验证功能;
- 继续在 IDE 编写部分代码;
- 然后在命令行关闭上一个进程,输入
go run main.go
重新启动进程; - 验证功能;
- 循环往复...
可以看到,启动进程是一个非常重复的行为,在一天的开发工作中可能会重复上百次。
那么,能否做到自动化这个行为?当你修改代码的时候,有个工具自动帮你重启进程,你只需要专注于修改代码和验证功能。
YES!这就是 Air!
Air 是一个在开发中实时重启 Go 项目的命令行工具,只需要在项目根目录运行 air
命令就可以专注于代码,不需要手动重启运行最新的项目代码。
安装
# 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
阡陌执行安装脚本会失败,直接下载编译好的二进制文件安装:
wget --no-check-certificate https://github.com/cosmtrek/air/releases/download/v1.27.3/air_1.27.3_linux_amd64 -O air
chmod +x air
mv air /usr/local/bin/
启动
# 在项目目录下执行
air
# 后台启动
nohup air > air.log 2>&1 < /dev/null &
配置
# Config file for [Air](https://github.com/cosmtrek/air) in TOML format
# Working directory
# . or absolute path, please note that the directories following must be under root.
root = "."
tmp_dir = "tmp"
[build]
# Just plain old shell command. You could use `make` as well.
# 默认执行的编译命令
cmd = "go build -o ./tmp/main ."
# Binary file yields from `cmd`.
bin = "tmp/main"
# Customize binary.
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
# Watch these filename extensions.
include_ext = ["go", "tpl", "tmpl", "html"]
# Ignore these filename extensions or directories.
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
# Watch these directories if you specified.
include_dir = []
# Exclude files.
exclude_file = []
# Exclude specific regular expressions.
exclude_regex = []
# Exclude unchanged files.
exclude_unchanged = true
# Follow symlink for directories
follow_symlink = true
# This log file places in your tmp_dir.
log = "air.log"
# It's not necessary to trigger build each time file changes if it's too frequent.
delay = 1000 # ms
# Stop running old binary when build errors occur.
stop_on_error = true
# Send Interrupt signal before killing process (windows does not support this feature)
send_interrupt = false
# Delay after sending Interrupt signal
kill_delay = 500 # ms
[log]
# Show log time
time = false
[color]
# Customize each part's color. If no color found, use the raw app log.
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"
[misc]
# Delete tmp directory on exit
clean_on_exit = true
踩坑
在软链接目录下会失效。