天天看点

Golang:使用air实现gin应用的live reload热重载

热重载 Go 应用的工具

文档

  • https://github.com/cosmtrek/air
  • https://github.com/cosmtrek/air/blob/master/README-zh_cn.md
  • 相似工具 https://github.com/gravityblast/fresh

安装依赖

go mod init go-gin-demo

go get -u github.com/cosmtrek/air
go get -u github.com/gin-gonic/gin
           

新建一个gin应用

main.go

package main

import (
    "net/http"

    "github.com/gin-gonic/gin"
)

func main() {
    app := gin.Default()

    app.GET("/", func(ctx *gin.Context) {
        ctx.String(http.StatusOK, "hello")
    })

    // 监听并在 http://127.0.0.1:8080 上启动服务
    app.Run()
}

           

启动应用

# 直接启动
go run main.go

# 使用air启动
air