字数 965,阅读大约需 5 分钟
Gin使用air热重载,超级便捷靠谱的使用教程
什么是air
Air - Live reload for Go apps
1. 当我用 Go 和 gin 框架开发网站时,gin 缺乏实时重载的功能是令人遗憾的。我曾经尝试过 fresh ,但是它用起来不太灵活。
2. Air 是为 Go 应用开发设计的另外一个热重载的命令行工具。只需在项目根目录下输入 air,然后把它放在一边,专注于代码即可。
什么是热重载
Live Reloading是一项功能,它使您的开发环境能够监视代码的更改并自动重新加载应用程序。当您保存文件时,当检测到源码文件变更,会重新构建应用程序并重新启动它。这样就避免了您在每次更改后手动停止服务、重新构建应用程序并再次启动的过程。
Live Reloading可以显著加快开发过程。对于习惯于在运行代码之前编译代码的Go开发人员来说,实时重新加载意味着改进的工作流程和即时反馈。其益处包括:
1. 提高生产率:开发人员无需手动重新构建即可保持专注。
2. 节省时间:实时重新加载减少了在正在运行应用程序中看到代码更改所需的时间。
3. 错误检测:即时重建和重启使开发人员能够更快地检测和纠正错误。
4. 流畅工作流:无缝集成到开发过程中,使工作流更加流畅和高效。
安装
1. go install github.com/air-verse/air@latest
2. air/README-zh_cn.md at master · air-verse/air · GitHub[1]
使用
.air.toml是Air的配置文件,配置在代码仓库的根目录即可
root = "." # 项目根目录
testdata_dir = "testdata" # 测试数据目录
tmp_dir = "tmp" # 临时文件目录
[build] # 构建配置
args_bin = [] # 传递给构建的参数
bin = "./tmp/main" # 构建后的二进制文件路径
cmd = "go build -o ./tmp/main ." # 构建命令
delay = 1000 # 启动延迟 (ms)
exclude_dir = ["tmp", "docs"] # 排除的目录
exclude_file = [] # 排除的文件
exclude_regex = ["_test.go"] # 排除的正则表达式
exclude_unchanged = false # 是否排除未更改的文件
follow_symlink = false # 是否跟随符号链接
full_bin = "" # 完整的二进制文件路径
include_dir = [] # 包含的目录
include_ext = ["go", "tpl", "tmpl", "html"] # 包含的文件扩展名
include_file = [] # 包含的文件
kill_delay = "0s" # 杀死进程的延迟
log = "build-errors.log" # 构建错误日志
poll = false # 是否使用轮询
poll_interval = 0 # 轮询间隔
post_cmd = [] # 构建后执行的命令
pre_cmd = [] # 构建前执行的命令
rerun = false # 是否重新运行
rerun_delay = 500 # 重新运行延迟
send_interrupt = false # 是否发送中断信号
stop_on_error = true # 发生错误时是否停止
[color] # 颜色配置
app = "" # 应用颜色
build = "yellow" # 构建颜色
main = "magenta" # 主程序颜色
runner = "green" # 运行器颜色
watcher = "cyan" # 监视器颜色
[log] # 日志配置
main_only = false # 是否只显示主程序日志
silent = false # 是否静默
time = false # 是否显示时间
[misc] # 其他配置
clean_on_exit = false # 退出时是否清理
[proxy] # 代理配置
app_port = 0 # 应用端口
enabled = false # 是否启用代理
proxy_port = 0 # 代理端口
[screen] # 屏幕配置
clear_on_rebuild = false # 重新构建时是否清除屏幕
keep_scroll = true # 是否保持滚动位置
启动air,输出如下
~/Code/deployment/message_nest/Message-Push-Nest (dev_daixingu*) » air gudaixin@gudaixindeMac-mini
__ _ ___
/ /\ | | | |_)
/_/--\ |_| |_| \_ v1.62.0, built with Go go1.25.0
watching .
watching conf
building...
^Ccleaning...
see you again~
引用链接
[1]
air/README-zh_cn.md at master · air-verse/air · GitHub: https://github.com/air-verse/air/blob/master/README-zh_cn.md
评论区