gin
gin copied to clipboard
Support Two or More Http Methods in One Function?
Description
Why Gin
doesn't have something like this in Echo
?
https://github.com/labstack/echo/blob/ec92fedf21e817d2d52004a4178292404beb9eaa/group.go#L87
So we can have something like:
func main() {
g := gin.Default()
g.Match([]string{"GET", "POST", "PUT"}, "/hello/:name", handlerFunc)
}
Would it be better than this?
func main() {
g := gin.Default()
g.GET("/hello/:name", handlerFunc).POST("/hello/:name", handlerFunc).PUT("/hello/:name", handlerFunc)
}
...to prevent ... boilerplate? Thanks,
you maybe use g.Any,it support any method.maybe help you
@dxgzg gin.Any()
will bring all http methods. What if I just want "GET" and "POST" only!!
easy, just write a high-order function to wrap it, you do not need Gin to support it
@Albert-Gao Can I have some code examples?
you can create your own struct wrap the router, and attach the PostAndGet method for a more idiomatic golang experience, but sounds overkill for just a few more helper functions.
@KiddoV
Interesting! Thanks!
Since we already had .Match()
(https://pkg.go.dev/github.com/gin-gonic/gin#RouterGroup.Match).
I am closing this issue!