在前边的开发中,我们对项目进行了一些初始化工作,但是我们发现每次修改之后想要看到效果都需要ctr+c
去停止项目并执行go run main.go start
去运行项目,那能不能省事一点,在我们编辑并保存文件时项目自动重新加载呢?
Air
Air 是为 Go 应用开发设计的另外一个热重载的命令行工具。只需在你的项目根目录下输入
air
,然后把它放在一边,专注于你的代码即可。
这是Air
的官方介绍,它有多种安装方式:
直接安装(推荐)
go install github.com/air-verse/air
安装之后可以在GOPATH/bin
目录下看到一个air可执行文明,运行:
➜ ~ air -v
__ _ ___
/ /\ | | | |_)
/_/--\ |_| |_| \_ , built with Go
说明安装成功
使用install.sh安装
# binary 文件会是在 $(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
还有其他多种安装方式,我们使用第一种直接安装,安装之后在项目下执行:
➜ eve_api git:(main) ✗ air
__ _ ___
/ /\ | | | |_)
/_/--\ |_| |_| \_ , built with Go
watching .
watching app
watching app/api
watching app/api/admin
watching app/api/app
watching app/event
watching app/event/entity
watching app/middleware
watching app/model
...
发现是执行了go run main.go
命令,但是我们的http服务没有启动,这是因为我们的http服务启动的命令是go run main.go start
,后边还需要加参数,air也支持加参数,运行:
➜ eve_api git:(main) ✗ air start
__ _ ___
/ /\ | | | |_)
/_/--\ |_| |_| \_ , built with Go
watching .
watching app
watching app/api
watching app/api/admin
watching app/api/app
...
watching internal/tool
!exclude tmp
building...
running...
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /admin/a --> eve/app/route.genAdminRouter.func1 (1 handlers)
[GIN-debug] GET /app/b --> eve/app/route.genAppRouter.func1 (1 handlers)
可以看到http服务已经启动,然后我们随便编辑项目中一个文件并保存,可以看到http服务会重启,如Air官方介绍的,只需要输入air,其他即可放在一边,专心代码~
总结
- air 安装
- 功过air热更新项目
commit-hash: 249c143