Air 是一个开发环境下的 Go Web 应用程序自动重新加载工具。当你对你的项目进行更改并保存时,它会自动重新构建和重新加载应用程序。Air 支持 Windows、macOS 和 Linux。这里以 Windows 作演示。
下载
go get -u github.com/cosmtrek/air
配置环境变量
-
配置环境变量需要 exe 执行文件。
首先进入 ari 目录,在 GOMODCACHE 目录下
\github.com\cosmtrek\air@v1.52.0
,查看方式如下:目录为 G:\Go\pkg\mod\github.com\cosmtrek\air@v1.52.0
C:\Users\Administrator>Go env ... set GOMODCACHE=G:\Go\pkg\mod ...
-
然后执行
go build .
生成air.exe
文件;G:\Go\pkg\mod\github.com\cosmtrek\air@v1.52.0>go build . go: downloading github.com/fatih/color v1.14.1 ....
-
配置 ari.exe 到环境变量
-
重启命令窗口, 执行
air -v
,如下展示说明配置完成。C:\Users\Administrator>air -v __ _ ___ / /\ | | | |_) /_/--\ |_| |_| \_ (devel), built with Go go1.22.2
运行 Air
-
普通项目。如 gin,进入你GO项目根目录,执行
air
命令,执行成功完毕。gin 示例如下:watching xxx为监听的目录。
PS G:\Go\src\go_gin> air __ _ ___ / /\ | | | |_) /_/--\ |_| |_| \_ (devel), built with Go go1.22.2 mkdir G:\Go\src\go_gin\tmp watching . watching configs ... !exclude tmp building... running...
-
go-admin
air server
-
Go项目根目录中配置air.conf文件
# [Air](https://github.com/cosmtrek/air) TOML 格式的配置文件 # 工作目录 # 使用 . 或绝对路径,请注意 `tmp_dir` 目录必须在 `root` 目录下 root = "." tmp_dir = "tmp" [build] # 只需要写你平常编译使用的shell命令。你也可以使用 `make` # Windows平台示例: cmd = "go build -o ./tmp/main.exe ." cmd = "go build -o ./tmp/main.exe ." # 由`cmd`命令得到的二进制文件名 # Windows平台示例:bin = "tmp/main.exe" bin = "tmp/main.exe" # 自定义执行程序的命令,可以添加额外的编译标识例如添加 GIN_MODE=release # Windows平台示例:full_bin = "./tmp/main.exe" # Linux平台示例:full_bin = "APP_ENV=dev APP_USER=air ./tmp/main.exe" full_bin = "./tmp/main.exe" # 监听以下文件扩展名的文件. include_ext = ["go", "tpl", "tmpl", "html"] # 忽略这些文件扩展名或目录 exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"] # 监听以下指定目录的文件 include_dir = [] # 排除以下文件 exclude_file = [] # 如果文件更改过于频繁,则没有必要在每次更改时都触发构建。可以设置触发构建的延迟时间 delay = 1000 # ms # 发生构建错误时,停止运行旧的二进制文件。 stop_on_error = true # air的日志文件名,该日志文件放置在你的`tmp_dir`中 log = "air_errors.log" [log] # 显示日志时间 time = true [color] # 自定义每个部分显示的颜色。如果找不到颜色,使用原始的应用程序日志。 main = "magenta" watcher = "cyan" build = "yellow" runner = "green" [misc] # 退出时删除tmp目录 clean_on_exit = true