gin使用Air实时加载

Air能够实时监听项目的代码文件,在代码发生变更之后自动重新编译并执行,大大提高gin框架项目的开发效率。

1. 为什么需要实时加载?

当修改了项目代码之后,程序能够自动重新加载并执行(live-reload),这在日常的开发阶段是十分方便的。

在使用Go语言的gin框架在本地做开发调试的时候,经常需要在变更代码之后频繁的按下Ctrl+C停止程序并重新编译再执行,这样就不是很方便。

2. Air特性

  • 彩色日志输出
  • 自定义构建或二进制命令
  • 支持忽略子目录
  • 启动后支持监听新目录
  • 更好的构建过程

3. 安装Air

3.1 通过Go安装air
    go get -u github.com/cosmtrek/air
3.2 编译air生成命令

不知道3.1安装的air在什么地方,可以通过下面命令查看位置,找到GOPATH

C:\Users\leell>go env
set GOPATH=C:\Users\leell\go

现在就可以进去找air,并且编译生成工具air.exe,我的开发环境是windows,如果你想编译其它平台的执行命令自行设置编译平台。beego的安装及bee工具的使用-CSDN博客

C:\Users\leell\go\pkg\mod\github.com\cosmtrek\air@v1.49.0>go build
go: downloading golang.org/x/sys v0.5.0
go: downloading github.com/pelletier/go-toml/v2 v2.0.6

将上面生成的air.exe移动到GOPATH/bin,然后查看命令信息。

C:\Users\leell\go>air -v

  __    _   ___
 / /\  | | | |_)
/_/--\ |_| |_| \_ (devel), built with Go go1.21.6

提示:我是在我的环境变量PATH配置了GOPATH/bin的,注意不要忘了。

4.开始使用Air

上面的准备工作已经做足了,现在就可以开始继续真正的工作了。

进入你的项目中,如果你的项目中没有.air.conf,新建一个.air.conf空文件。

G:\github\go-cms>air -c .air.conf

  __    _   ___
 / /\  | | | |_)
/_/--\ |_| |_| \_ (devel), built with Go go1.21.6

watching .
watching api
watching api\v1
.......

执行过程中会报错,如果报错就执行下面的命令,安装需要的依赖包

go mod tidy

再次执使用air的命令

G:\github\go-cms>air -c .air.conf

  __    _   ___
 / /\  | | | |_)
/_/--\ |_| |_| \_ (devel), built with Go go1.21.6
....
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    /static/*filepath         --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (4 handlers)
[GIN-debug] HEAD   /static/*filepath         --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (4 handlers)
[GIN-debug] GET    /admin/*filepath          --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (4 handlers)
[GIN-debug] HEAD   /admin/*filepath          --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (4 handlers)
[GIN-debug] GET    /favicon.ico              --> gocms/routes.InitRouter.(*RouterGroup).StaticFile.func3 (4 handlers)
[GIN-debug] HEAD   /favicon.ico              --> gocms/routes.InitRouter.(*RouterGroup).StaticFile.func3 (4 handlers)
[GIN-debug] GET    /                         --> gocms/routes.InitRouter.func1 (4 handlers)
[GIN-debug] GET    /admin                    --> gocms/routes.InitRouter.func2 (4 handlers)
[GIN-debug] GET    /api/v1/admin/users       --> gocms/api/v1.GetUsers (5 handlers)
[GIN-debug] PUT    /api/v1/user/:id          --> gocms/api/v1.EditUser (5 handlers)
[GIN-debug] DELETE /api/v1/user/:id          --> gocms/api/v1.DeleteUser (5 handlers)
[GIN-debug] PUT    /api/v1/admin/changepw/:id --> gocms/api/v1.ChangeUserPassword (5 handlers)
[GIN-debug] GET    /api/v1/admin/category    --> gocms/api/v1.GetCate (5 handlers)
[GIN-debug] POST   /api/v1/category/add      --> gocms/api/v1.AddCategory (5 handlers)
[GIN-debug] PUT    /api/v1/category/:id      --> gocms/api/v1.EditCate (5 handlers)
[GIN-debug] DELETE /api/v1/category/:id      --> gocms/api/v1.DeleteCate (5 handlers)
[GIN-debug] GET    /api/v1/admin/article/info/:id --> gocms/api/v1.GetArtInfo (5 handlers)
[GIN-debug] GET    /api/v1/admin/article     --> gocms/api/v1.GetArt (5 handlers)
[GIN-debug] POST   /api/v1/article/add       --> gocms/api/v1.AddArticle (5 handlers)
[GIN-debug] PUT    /api/v1/article/:id       --> gocms/api/v1.EditArt (5 handlers)
[GIN-debug] DELETE /api/v1/article/:id       --> gocms/api/v1.DeleteArt (5 handlers)
[GIN-debug] POST   /api/v1/upload            --> gocms/api/v1.UpLoad (5 handlers)
[GIN-debug] GET    /api/v1/admin/profile/:id --> gocms/api/v1.GetProfile (5 handlers)
[GIN-debug] PUT    /api/v1/profile/:id       --> gocms/api/v1.UpdateProfile (5 handlers)
[GIN-debug] GET    /api/v1/comment/list      --> gocms/api/v1.GetCommentList (5 handlers)
[GIN-debug] DELETE /api/v1/delcomment/:id    --> gocms/api/v1.DeleteComment (5 handlers)
[GIN-debug] PUT    /api/v1/checkcomment/:id  --> gocms/api/v1.CheckComment (5 handlers)
[GIN-debug] PUT    /api/v1/uncheckcomment/:id --> gocms/api/v1.UncheckComment (5 handlers)
[GIN-debug] POST   /api/v1/user/add          --> gocms/api/v1.AddUser (4 handlers)
[GIN-debug] GET    /api/v1/user/:id          --> gocms/api/v1.GetUserInfo (4 handlers)
[GIN-debug] GET    /api/v1/users             --> gocms/api/v1.GetUsers (4 handlers)
[GIN-debug] GET    /api/v1/category          --> gocms/api/v1.GetCate (4 handlers)
[GIN-debug] GET    /api/v1/category/:id      --> gocms/api/v1.GetCateInfo (4 handlers)
[GIN-debug] GET    /api/v1/article           --> gocms/api/v1.GetArt (4 handlers)
[GIN-debug] GET    /api/v1/article/list/:id  --> gocms/api/v1.GetCateArt (4 handlers)
[GIN-debug] GET    /api/v1/article/info/:id  --> gocms/api/v1.GetArtInfo (4 handlers)
[GIN-debug] POST   /api/v1/login             --> gocms/api/v1.Login (4 handlers)
[GIN-debug] POST   /api/v1/loginfront        --> gocms/api/v1.LoginFront (4 handlers)
[GIN-debug] GET    /api/v1/profile/:id       --> gocms/api/v1.GetProfile (4 handlers)
[GIN-debug] POST   /api/v1/addcomment        --> gocms/api/v1.AddComment (4 handlers)
[GIN-debug] GET    /api/v1/comment/info/:id  --> gocms/api/v1.GetComment (4 handlers)
[GIN-debug] GET    /api/v1/commentfront/:id  --> gocms/api/v1.GetCommentListFront (4 handlers)
[GIN-debug] GET    /api/v1/commentcount/:id  --> gocms/api/v1.GetCommentCount (4 handlers)
[GIN-debug] Listening and serving HTTP on :3000

从上面的显示可以看出服务已经启动成功了,可以看出列举出了所有接口地址,所有监听的文件。

5.验证air实时加载情况

修改之前显示的提示

修改之后:

utils\errmsg\errmsg.go has changed
building...
!exclude .git
!exclude .git
running...

将会重新加载文件,并且启动进程

air_example.conf示例

完整的air_example.conf示例配置如下,可以根据自己的需要修改.air.conf。

# [Air](https://github.com/cosmtrek/air) TOML 格式的配置文件

# 工作目录
# 使用 . 或绝对路径,请注意 `tmp_dir` 目录必须在 `root` 目录下
root = "."
tmp_dir = "tmp"

[build]
# 只需要写你平常编译使用的shell命令。你也可以使用 `make`
cmd = "go build -o ./tmp/main ."
# 由`cmd`命令得到的二进制文件名
bin = "tmp/main"
# 自定义的二进制,可以添加额外的编译标识例如添加 GIN_MODE=release
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
# 监听以下文件扩展名的文件.
include_ext = ["go", "tpl", "tmpl", "html"]
# 忽略这些文件扩展名或目录
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
# 监听以下指定目录的文件
include_dir = []
# 排除以下文件
exclude_file = []
# 如果文件更改过于频繁,则没有必要在每次更改时都触发构建。可以设置触发构建的延迟时间
delay = 1000 # ms
# 发生构建错误时,停止运行旧的二进制文件。
stop_on_error = true
# air的日志文件名,该日志文件放置在你的`tmp_dir`中
log = "air_errors.log"

[log]
# 显示日志时间
time = true

[color]
# 自定义每个部分显示的颜色。如果找不到颜色,使用原始的应用程序日志。
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"

[misc]
# 退出时删除tmp目录
clean_on_exit = true

参考文章:

https://www.fansimao.com/928949.html 

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流光影下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值