go项目使用air 热重载

186 阅读1分钟

使用 air 热重载

  1. curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin (官方推荐)
  2. 装完之后可能在终端运行 air 可能会报错zsh: command not found: air。解决:以oh-my-zsh为例:在控制台使用vim ~/.zshrcexport PATH=$HOME/go/bin:$PATH添加到环境变量中保存退出之后 刷新一下zshrc文件 source ~/.zshrc
  3. 终端输入 air -v 即可查看能否正常使用
  4. 如何配置?在项目根目录创建 air.toml 文件将下面代码复制进去即可(GPT生成的,能使~)
  5. 终端运行 air即可,这样代码变更就可以自动重新运行项目
# air.toml
# A general configuration for the Runner.
[build]
cmd = "go build -o ./tmp/main" # Replace this with your build command
binary = "./tmp/main" # Replace this with your binary path
root = "." # Replace this with your root directory
exclude_dir = "assets" # Exclude directories, if needed

# You can override the default working directory. Default is ".".
# cwd = ""

[log]
color = true
time = true
request = false
error = true
modify = true

[misc]
log = "main" # Log only the "main" source code. Default value is "" (empty string), meaning log all source codes.

[[event]]
# Feel free to disable any event by commenting out or deleting its configuration.
# You can also change the watch method from the default "polling" to "osnotify" (on Linux)
# or "fsevents" (on macOS).
# recursive = true # Also watch every directory recursively
# delay = 1000 # Latency while watching. If the value is less than 100, then the system default will be used.
include = ["*.go"] # Watch only .go files

# auto test
[tester]
cmd = "go test -v ./..."
  1. 还有个问题就是 运行 air 或者 go run main.go 如果使用的是 :8080就会有下面的警告提示,就很烦。所以你只需要把这个补齐就不会报错啦 127.0.0.1:8080

image.png 详情参考GitHub: air