gin框架
初步
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
app := gin.Default()
app.StaticFile("/favicon.ico", "./favicon.ico")
app.GET("/hello", func(c *gin.Context) {
c.String(http.StatusOK, "hello gin")
})
app.Run(":8080")
}
中间件
app.Use
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
app := gin.Default()
// 全局中间件
//
app.Use(gin.Logger())
// Recovery 中间件会recover任何panic。如果有panic的话,会写入500。
app.Use(gin.Recovery())
app.StaticFile("/favicon.ico", "./favicon.ico")
app.GET("/hello", func(c *gin.Context) {
c.String(http.StatusOK, "hello gin")
})
app.Run(":8080")
}