-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unnecessary engine.With(opts...) call #3967
Comments
Because |
If we pass the opt to New, will it do the job right? |
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())
} |
If there are no further issues, you might consider closing the issue. Thank you very much. 😁 |
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? |
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
.The text was updated successfully, but these errors were encountered: