go专题airgo实时重载air
安安Go Air 详细讲解
Air 是一个用于 Go 应用程序的实时重载工具,可以在代码更改时自动重建和重启应用程序,极大提高开发效率。
1. 安装
方式一:使用 Go 安装
1
| go install github.com/cosmtrek/air@latest
|
方式二:直接下载二进制文件
1 2 3 4 5 6 7 8
| curl -fLo air https://git.io/linux_air
curl -fLo air https://git.io/darwin_air
curl -fLo air.exe https://git.io/windows_air
|
方式三:使用包管理器
1 2 3 4 5 6 7 8
| brew install air
scoop install air
yay -S air
|
2. 基本使用
快速开始
1 2 3 4 5 6 7 8
| air
air -c .air.toml
air init
|
项目结构示例
1 2 3 4 5 6
| myproject/ ├── main.go ├── go.mod ├── .air.toml # Air 配置文件 └── cmd/ └── server.go
|
3. 配置文件详解
初始化默认配置
这会生成一个 .air.toml 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| root = "." testdata_dir = "testdata" tmp_dir = "tmp"
[build]
include_ext = ["go", "tpl", "tmpl", "html"] exclude_dir = ["assets", "tmp", "vendor", "testdata"] include_dir = [] exclude_file = []
include_ext = ["go", "tpl", "tmpl", "html"]
exclude_dir = ["assets", "tmp", "vendor", "testdata", ".git"]
include_dir = []
exclude_file = []
cmd = "go build -o ./tmp/main ."
bin = "tmp/main"
full_bin = "./tmp/main"
args_bin = []
log = "air.log"
stop_on_root = false
send_interrupt = false interrupt_timeout = 5 kill_delay = 0
delay = 1000
[log]
time = false
[color]
main = "magenta" watcher = "cyan" build = "yellow" runner = "green"
[misc]
clean_on_exit = true
|
自定义配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
root = "."
tmp_dir = "tmp"
[build]
pre_cmd = ["go mod tidy"]
cmd = "go build -o ./tem/main ./cmd/main.go"
post_cmd = []
bin = "tem/main"
args_bin = []
include_ext = ["go", "tpl", "tmpl", "html", "yaml", "yml"]
exclude_dir = [ "assets", "tmp", "vendor", "frontend/node_modules", ".git", "_output", "logs" ]
include_dir = ["cmd", "internal", "pkg", "configs"]
include_file = []
exclude_file = []
exclude_regex = ["_test\\.go", "_mock\\.go"]
exclude_unchanged = true
follow_symlink = true
log = "air.log"
poll = false
poll_interval = 500
delay = 1000
stop_on_error = true
send_interrupt = true
kill_delay = 1000000000
rerun = true
rerun_delay = 1000
[log]
time = true
main_only = false
[color]
main = "magenta" watcher = "cyan" build = "yellow" runner = "green"
[misc]
clean_on_exit = true
[screen]
clear_on_rebuild = true
keep_scroll = true
[proxy] enabled = true proxy_port = 8090 app_port = 8080
|
4. 实际项目配置示例
Web 服务器项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| root = "." tmp_dir = "tmp"
[build] cmd = "go build -o ./tmp/main ./cmd/server" bin = "tmp/main" full_bin = "./tmp/main server --config=config.yaml" include_ext = ["go", "yaml", "yml", "json"] exclude_dir = ["tmp", "vendor", "testdata", "logs", ".git"] include_dir = ["cmd", "internal", "pkg", "configs"] log = "air.log" delay = 1000 stop_on_root = false send_interrupt = true
[log] time = true
[color] main = "blue" watcher = "cyan" build = "yellow" runner = "green"
[misc] clean_on_exit = false
|
微服务项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| root = "." tmp_dir = "tmp"
[build] cmd = "go build -ldflags '-s -w' -o ./tmp/main ./cmd/user-service" bin = "tmp/main" full_bin = "./tmp/main --env=dev" include_ext = ["go", "proto", "yaml"] exclude_dir = ["tmp", "vendor", "testdata", "dist", ".git"] include_dir = ["cmd", "internal", "pkg", "api", "config"] log = "air.log" delay = 1500 stop_on_root = true
[log] time = true
[color] main = "green" watcher = "blue" build = "magenta" runner = "yellow"
|
5. 集成到开发工作流
与 Makefile 集成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| .PHONY: dev build test
dev: air -c .air.toml
build: go build -o bin/server ./cmd/server
test: go test ./...
run: go run ./cmd/server
clean: rm -rf tmp bin
|
与 Docker 集成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| FROM golang:1.19-alpine
WORKDIR /app
RUN go install github.com/cosmtrek/air@latest
COPY .air.toml ./ COPY go.mod go.sum ./ RUN go mod download
COPY . .
CMD ["air", "-c", ".air.toml"]
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| version: '3.8' services: app: build: context: . dockerfile: Dockerfile.dev ports: - "8080:8080" volumes: - .:/app - /app/tmp environment: - GO_ENV=development
|
6. 高级配置技巧
环境特定配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| root = "."
[build]
cmd = "go build -o ./tmp/main ." bin = "tmp/main"
[build.dev] full_bin = "./tmp/main --env=dev --debug=true" args_bin = ["--port=3000"]
[build.test] full_bin = "./tmp/main --env=test" args_bin = ["--port=8080"]
[log] time = true
[color] main = "magenta" watcher = "cyan" build = "yellow" runner = "green"
|
多命令支持
1 2 3 4 5 6 7 8 9 10
| [build] cmd = "go generate && go build -o ./tmp/main ." bin = "tmp/main" full_bin = "./tmp/main"
[build] cmd = ["go", "generate", "&&", "go", "build", "-o", "./tmp/main", "."] bin = "tmp/main"
|
7. 常见问题解决
go版本问题
air最新版需要go版本>=1.25
1 2 3 4 5 6 7 8
| export GOSUMDB=on
export GOSUMDB=sum.golang.org export GOPROXY=https://goproxy.cn,direct
go install github.com/air-verse/air@latest
|
权限问题
1 2 3 4 5 6
| chmod +x tmp/main
[build] cmd = "go build -o ./tmp/main . && chmod +x ./tmp/main"
|
端口占用问题
1 2 3 4 5
| [build]
send_interrupt = true interrupt_timeout = 5 kill_delay = 1000
|
处理静态文件
1 2 3 4
| [build]
include_ext = ["go", "html", "tpl", "tmpl", "css", "js"] exclude_dir = ["tmp", "vendor", "testdata", "dist"]
|
8. 性能优化配置
最小化监视范围
1 2 3 4 5 6 7 8 9 10 11
| [build]
include_dir = ["cmd", "internal", "pkg"] exclude_dir = [ "tmp", "vendor", "testdata", "node_modules", "dist", ".git", "docs", "migrations" ]
delay = 500
|
优化构建命令
1 2 3 4 5 6 7
| [build]
cmd = "go build -tags=jsoniter -o ./tmp/main ." bin = "tmp/main"
|
9. 与其他工具集成
与 Reflex 比较
1 2 3
| go get github.com/cespare/reflex reflex -r '\.go$' -s -- go run main.go
|
与 Nodemon 比较
1 2 3
|
nodemon --exec go run main.go --ext go
|
10. 最佳实践
- 版本控制:将
.air.toml 加入版本控制
- 环境配置:为不同环境创建不同的配置文件
- 性能优化:合理设置监视目录,避免不必要的文件监视
- 错误处理:配置适当的超时和中断设置
- 日志管理:在生产环境中禁用 Air,仅用于开发
示例项目结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| myapp/ ├── .air.toml ├── .air.prod.toml ├── Makefile ├── Dockerfile ├── Dockerfile.dev ├── cmd/ │ └── server/ │ └── main.go ├── internal/ │ └── app/ ├── pkg/ │ └── utils/ └── go.mod
|
Air 是 Go 开发中非常有用的工具,可以显著提高开发效率,特别是在需要频繁修改和测试的 Web 开发场景中。
[up主专用,视频内嵌代码贴在这]