golang实现Go应用实时热重载开发工具插件库air的使用

Golang实现Go应用实时热重载开发工具插件库Air的使用

☁️ Air - Go应用实时热重载工具

Air是一个用于Go应用程序开发的实时重载命令行工具。只需在项目根目录运行air命令,它就会监控代码变化并自动重新编译运行你的应用。

功能特性

  • 彩色日志输出
  • 自定义构建命令
  • 支持排除子目录
  • 启动后可以添加新的监控目录
  • 优化的构建过程

安装方法

推荐使用go install安装

go install github.com/air-verse/air@latest

通过install.sh安装

# 二进制文件会安装到$(go env GOPATH)/bin/air
curl -sSfL https://raw.githubusercontent.com/air-verse/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin

# 或者安装到./bin/目录
curl -sSfL https://raw.githubusercontent.com/air-verse/air/master/install.sh | sh -s

air -v

使用示例

基本使用

  1. 首先进入你的项目目录:
cd /path/to/your_project
  1. 初始化配置文件(可选):
air init

这会生成一个默认的.air.toml配置文件。

  1. 启动Air:
air

自定义配置示例

你可以在命令行中直接覆盖配置参数:

air --build.cmd "go build -o bin/api cmd/run.go" --build.bin "./bin/api"

或者使用逗号分隔列表参数:

air --build.cmd "go build -o bin/api cmd/run.go" --build.bin "./bin/api" --build.exclude_dir "templates,build"

运行时参数

你可以传递参数给构建后的二进制文件:

# 会运行 ./tmp/main bench
air bench

# 会运行 ./tmp/main server --port 8080
air server --port 8080

使用--分隔Air参数和程序参数:

# 会运行 ./tmp/main -h
air -- -h

# 使用自定义配置并传递-h参数给程序
air -c .air.toml -- -h

Docker使用示例

直接运行

docker run -it --rm \
    -w "<PROJECT>" \
    -e "air_wd=<PROJECT>" \
    -v $(pwd):<PROJECT> \
    -p <PORT>:<APP SERVER PORT> \
    cosmtrek/air
    -c <CONF>

在shellrc中添加函数

air() {
  podman/docker run -it --rm \
    -w "$PWD" -v "$PWD":"$PWD" \
    -p "$AIR_PORT":"$AIR_PORT" \
    docker.io/cosmtrek/air "$@"
}

配置示例

.air.toml配置文件示例:

[build]
  cmd = "go build -o ./tmp/main ."
  bin = "./tmp/main"
  exclude_dir = ["assets", "tmp", "vendor"]
  include_ext = ["go", "tpl", "tmpl", "html"]
  delay = 1000 # ms
  stop_on_error = true

[log]
  time = false

[color]
  main = "magenta"
  watcher = "cyan"
  build = "yellow"
  runner = "green"

常见问题

命令找不到问题

如果遇到"command not found: air"错误,请确保PATH设置正确:

export GOPATH=$HOME/xxxxx
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
export PATH=$PATH:$(go env GOPATH)/bin

仅热编译不运行

如果你想只编译不运行,可以这样配置:

[build]
  cmd = "/usr/bin/true"

静态文件修改自动刷新浏览器

确保:

  1. 静态文件在include_dirinclude_extinclude_file
  2. HTML有</body>标签
  3. 启用代理配置:
[proxy]
  enabled = true
  proxy_port = <air proxy port>
  app_port = <your server port>

开发

如果你想参与Air的开发:

# Fork项目
# 克隆
mkdir -p $GOPATH/src/github.com/cosmtrek
cd $GOPATH/src/github.com/cosmtrek
git clone git@github.com:<YOUR USERNAME>/air.git

# 安装依赖
cd air
make ci

# 探索并开始开发
make install

Air是一个强大的Go开发工具,可以显著提高开发效率,避免手动停止和重启服务的麻烦。


更多关于golang实现Go应用实时热重载开发工具插件库air的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang实现Go应用实时热重载开发工具插件库air的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Golang 实时热重载工具 Air 的使用指南

Air 是一个用于 Go 应用的实时热重载开发工具,可以在代码修改后自动重新编译和重启应用,极大提高开发效率。

安装 Air

# 使用 Go 安装
go install github.com/cosmtrek/air@latest

# 或者使用 curl (Linux/macOS)
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s

基本使用

  1. 在项目根目录下初始化 Air 配置:
air init

这会生成一个 .air.toml 配置文件。

  1. 启动 Air:
air

配置示例

以下是典型的 .air.toml 配置:

# [.air.toml]
root = "."
tmp_dir = "tmp"

[build]
cmd = "go build -o ./tmp/main ."
bin = "tmp/main"
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
include_ext = ["go", "tpl", "tmpl", "html"]
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
include_dir = []
exclude_file = []
log = "build-errors.log"
delay = 1000 # ms
stop_on_error = true
send_interrupt = true
kill_delay = 500 # ms

[log]
time = false

[color]
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"

[misc]
clean_on_exit = true

高级配置选项

  1. 自定义构建命令
[build]
cmd = "go build -tags=dev -o ./tmp/main ."
  1. 环境变量
[build]
full_bin = "ENV=dev DB_HOST=localhost ./tmp/main"
  1. 忽略特定目录/文件
[build]
exclude_dir = ["vendor", "node_modules", "dist"]
exclude_file = ["*.test.go"]

实际开发示例

假设我们有一个简单的 web 服务:

// main.go
package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello, Air! (Version: v1.0)")
	})
	
	fmt.Println("Server running on :8080")
	http.ListenAndServe(":8080", nil)
}

使用 Air 开发流程:

  1. 启动 Air:
air
  1. 修改代码后保存,Air 会自动:
    • 检测文件变化
    • 重新编译应用
    • 停止旧进程
    • 启动新进程

集成到 Makefile

可以创建 Makefile 简化开发流程:

.PHONY: dev
dev:
	air

.PHONY: build
build:
	go build -o ./bin/app .

.PHONY: run
run:
	./bin/app

常见问题解决

  1. 端口占用问题

    • 确保应用正确处理 SIGINT 信号
    • 在配置中增加 kill_delay
  2. 构建失败

    • 检查 stop_on_error 设置
    • 查看 build-errors.log 文件
  3. 文件监视不工作

    • 检查 include_extexclude_dir 配置
    • 在 Linux 上可能需要增加文件监视限制:
      echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
      

与其他工具比较

工具 优点 缺点
Air 配置简单,功能全面 需要额外安装
Fresh 极简 功能较少
Gin 专为 HTTP 服务优化 不适用于非 HTTP 服务
Realize 功能强大 配置复杂

Air 因其平衡的配置方式和全面的功能成为许多 Go 开发者的首选热重载工具。

总结

Air 提供了简单高效的 Go 应用热重载解决方案,通过合理的配置可以适应各种开发场景。它的主要优势在于:

  1. 直观的 TOML 配置文件
  2. 灵活的构建和运行命令配置
  3. 详细的日志和错误处理
  4. 跨平台支持

对于需要频繁修改和测试的 Go 项目,Air 可以显著提升开发体验和效率。

回到顶部