Description
I was looking at the code and saw that in the Default method engine := New() where New method is returning engine.With(opts...) and again the Default method also returning engine.With(opts...) which I think is redundant engine.With
call 🤔.
Expectations
engine.With(opts...) is sufficient and Default should just return engine
.
Comment From: flc1125
Because Default
also needs to support opts
, and in Default
, some logic should prioritize opts
over the defaults
(such as middleware
).
Comment From: raashidanwar
If we pass the opt to New, will it do the job right?
Comment From: flc1125
Certainly. There are many ways to use it:
package main
import "github.com/gin-gonic/gin"
func main() {
router:=gin.New(
routes,
route(),
middlewares,
)
router.
With(middlewares).
GET("/v2", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "hello, world"})
})
router.Run(":8080")
}
func routes(e *gin.Engine) {
e.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "hello, world"})
})
}
func route() func(*gin.Engine) {
return func(e *gin.Engine) {
e.GET("/v1", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "hello, world"})
})
}
}
func middlewares(e *gin.Engine) {
e.Use(gin.Logger())
e.Use(gin.Recovery())
}
Comment From: flc1125
If there are no further issues, you might consider closing the issue. Thank you very much. 😁
Comment From: dukepan2005
Sorry, I agree with @raashidanwar. In my humble opinion, I didn't see any advantage of "With" function. It seems we can add router and middleware in good ways before.
There are many articles to explain the benefit of "the functional Options pattern", Sorry, but I didn't see it there. It seems make gin further away from simplicity.
@flc1125 Can you introduce more scene where the traditional way is clumsy?