go --- air自动重新编译

0、Air介绍

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

 

github地址:https://github.com/cosmtrek/air

 

1、安装

go install github.com/cosmtrek/air@latest

 

2、初始化

air init

 
默认生成配置文件.air.toml,内容如下:

root = "."
testdata_dir = "testdata"
# 日志存放位置
tmp_dir = "tmp"

[build]
  args_bin = []
  # 启动命令
  bin = "./tmp/main"
  # 编译
  cmd = "go build -o ./tmp/main ."
  delay = 1000
  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "html"]
  kill_delay = "0s"
  log = "build-errors.log"
  send_interrupt = false
  stop_on_error = true

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

[log]
  time = false

[misc]
  clean_on_exit = false

[screen]
  clear_on_rebuild = false

按需修改日志、编译文件位置、编译后执行命令即可

 

3、准备服务

一个简单的demo

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	r.GET("/hello", func(c *gin.Context) {
		c.String(http.StatusOK, "mio")
	})
	_ = r.Run(":9090")
}

 

4、启动服务

air

请添加图片描述

此时监听9090端口

 

5、测试实时加载

将端口号9090改为9091后保存,看终端中是否重新编译

请添加图片描述

修改保存后服务重新编译启动,监听端口号变为了9091,测试成功

 
如有不对,烦请指出,感谢~

参考链接:
https://www.liwenzhou.com/posts/Go/live_reload_with_air/
https://blog.miuyun.work/archives/16036007

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值