-
提供了更多的 handler 选择。
func(*gin.Context)func(*gin.Context) errorfunc(*gin.Context, *reqType) errorfunc(*gin.Context) (*respType, error)func(*gin.Context, *reqType) (*respType, error)
-
自动参数绑定。如何添加更多支持的 tag ?
headeruriformjson
-
默认使用中文的 validator。
-
通过 tag
default为reqType提供默认值,默认支持:string:default:"foo"[]byte:default:"bar"int:default:"10"float64:default:"10.0"bool:default:"true"time.Duration:default:"10s", 使用time.ParseDuration进行解析。time.Time: 使用time.RFC3339或者time.RFC3339Nano,支持now+{time.Duration},now-{time.Duration}- 使用
mapstructure支持自定义类型。
-
支持使用
zerolog覆盖以下gin的配置。gin.DefaultWritergin.DefaultErrorWritergin.DebugPrintRouteFunc
-
reqType支持下列钩子。BeforeBind(*gin.Context)AfterBind(*gin.Context)BeforeValidate(*gin.Context)AfterValidate(*gin.Context)
package main
import (
"github.com/fioepq9/helper"
"github.com/gin-gonic/gin"
)
type EchoRequest struct {
Message string `form:"message"`
}
type EchoResponse struct {
Message string `json:"message"`
}
// curl -X GET "http://localhost:8080/echo?message=hello"
// {"message": "hello"}
func main() {
e := gin.New()
r := helper.Gin().Router(e)
r.GET("/echo", func(c *gin.Context, req *EchoRequest) (resp *EchoResponse, err error) {
return &EchoResponse{Message: req.Message}, nil
})
if err := e.Run(":8080"); err != nil {
panic(err)
}
}