Hi all, for this article, I will keep note of how to use router group in Gin.
What is Router Group
Router Group is a function to extract routers for each group.
For example, if there are 2 features of the API, we can use Router Group to separate routers to another file and split the routers by features.
Code
main.go
package main |
routers/UserRouters.go
package routers |
Explanation
- We delete routing we’d added before and create a variable for the router group called
routerGroup
. - We call the
AddRouters
function from therouters
package and pass therouterGroup
as a parameter. - We add a new file called
UserRouters.go
in therouters
package and add some routing config in the functionAddRouters
.