作用

本地做开发调试的时候自动的重新监听文件的变化,然后进程项目代码的重新编译运行。

windows环境下, 集成到GIN

使用GO的方式安装:

第一步 安装:

  1. D:\code\go\xmly_flow_pro>go get -u github.com/cosmtrek/air

第二步 定义 .air.conf文件:

  1. # Config file for [Air](https://github.com/cosmtrek/air) in TOML format
  2. # Working directory
  3. # . or absolute path, please note that the directories following must be under root.
  4. root = "."
  5. tmp_dir = "tmp"
  6. [build]
  7. # Just plain old shell command. You could use `make` as well.
  8. cmd = "go build -o ./tmp/main ."
  9. # Binary file yields from `cmd`.
  10. bin = "tmp/main"
  11. # Customize binary.
  12. full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
  13. # Watch these filename extensions.
  14. include_ext = ["go", "tpl", "tmpl", "html"]
  15. # Ignore these filename extensions or directories.
  16. exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
  17. # Watch these directories if you specified.
  18. include_dir = []
  19. # Exclude files.
  20. exclude_file = []
  21. # This log file places in your tmp_dir.
  22. log = "air.log"
  23. # It's not necessary to trigger build each time file changes if it's too frequent.
  24. delay = 1000 # ms
  25. # Stop running old binary when build errors occur.
  26. stop_on_error = true
  27. # Send Interrupt signal before killing process (windows does not support this feature)
  28. send_interrupt = false
  29. # Delay after sending Interrupt signal
  30. kill_delay = 500 # ms
  31. [log]
  32. # Show log time
  33. time = false
  34. [color]
  35. # Customize each part's color. If no color found, use the raw app log.
  36. main = "magenta"
  37. watcher = "cyan"
  38. build = "yellow"
  39. runner = "green"
  40. [misc]
  41. # Delete tmp directory on exit
  42. clean_on_exit = true

第三步当前目录下,执行air 启动项目:

  1. D:\code\go\xmly_flow_pro>air -c .air.conf

错误提示:
image.png

修改配置文件信息:

  1. # [Air](https://github.com/cosmtrek/air) TOML 格式的配置文件
  2. # 工作目录
  3. # 使用 . 或绝对路径,请注意 `tmp_dir` 目录必须在 `root` 目录下
  4. root = "."
  5. tmp_dir = "tmp"
  6. [build]
  7. # 只需要写你平常编译使用的shell命令。你也可以使用 `make`
  8. # Windows平台示例: cmd = "go build -o ./tmp/main.exe ."
  9. cmd = "go build -o ./tmp/main ."
  10. # 由`cmd`命令得到的二进制文件名
  11. # Windows平台示例:bin = "tmp/main.exe"
  12. bin = "tmp/main"
  13. # 自定义执行程序的命令,可以添加额外的编译标识例如添加 GIN_MODE=release
  14. # Windows平台示例:full_bin = "./tmp/main.exe"
  15. full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
  16. # 监听以下文件扩展名的文件.
  17. include_ext = ["go", "tpl", "tmpl", "html"]
  18. # 忽略这些文件扩展名或目录
  19. exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
  20. # 监听以下指定目录的文件
  21. include_dir = []
  22. # 排除以下文件
  23. exclude_file = []
  24. # 如果文件更改过于频繁,则没有必要在每次更改时都触发构建。可以设置触发构建的延迟时间
  25. delay = 1000 # ms
  26. # 发生构建错误时,停止运行旧的二进制文件。
  27. stop_on_error = true
  28. # air的日志文件名,该日志文件放置在你的`tmp_dir`中
  29. log = "air_errors.log"
  30. [log]
  31. # 显示日志时间
  32. time = true
  33. [color]
  34. # 自定义每个部分显示的颜色。如果找不到颜色,使用原始的应用程序日志。
  35. main = "magenta"
  36. watcher = "cyan"
  37. build = "yellow"
  38. runner = "green"
  39. [misc]
  40. # 退出时删除tmp目录
  41. clean_on_exit = true

继续启动:
D:\code\go\xmly_flow_pro>air -c .air.conf
依然提示:
image.png
修改配置文件中存在

  1. # 自定义执行程序的命令,可以添加额外的编译标识例如添加 GIN_MODE=release
  2. # Windows平台示例:full_bin = "./tmp/main.exe" full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
  3. full_bin = "./tmp/main.exe"

再继续执行:

  1. D:\code\go\xmly_flow_pro>air -c .air.conf

成功的启动!

第4步验证热重载:

修改代码查看热更新成功!