gin icon indicating copy to clipboard operation
gin copied to clipboard

Support Two or More Http Methods in One Function?

Open KiddoV opened this issue 1 year ago • 6 comments

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,

KiddoV avatar Jul 30 '22 14:07 KiddoV

you maybe use g.Any,it support any method.maybe help you

dxgzg avatar Sep 02 '22 03:09 dxgzg

@dxgzg gin.Any() will bring all http methods. What if I just want "GET" and "POST" only!!

KiddoV avatar Sep 02 '22 19:09 KiddoV

easy, just write a high-order function to wrap it, you do not need Gin to support it

Albert-Gao avatar Sep 02 '22 22:09 Albert-Gao

@Albert-Gao Can I have some code examples?

KiddoV avatar Sep 03 '22 21:09 KiddoV

image

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

Albert-Gao avatar Sep 03 '22 22:09 Albert-Gao

Interesting! Thanks!

KiddoV avatar Sep 04 '22 01:09 KiddoV

Since we already had .Match() (https://pkg.go.dev/github.com/gin-gonic/gin#RouterGroup.Match). I am closing this issue!

KiddoV avatar Apr 03 '23 13:04 KiddoV