gin框架关闭控制台输出

臭大佬 2022-05-28 21:05:09 215
Go 
简介 gin框架关闭控制台输出

代码

package main

import (
    "io/ioutil"

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

func main() {
    // 禁止Gin的控制台输出
    gin.SetMode(gin.ReleaseMode)
    gin.DefaultWriter = ioutil.Discard
    r := gin.Default()
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
    r.Run() // listen and serve on 0.0.0.0:8080
}

关键代码

gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = ioutil.Discard