天天看點

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