import "github.com/gin-gonic/gin" func myMiddleware(c *gin.Context) { // do some work before passing control to the next handler c.Next() } func main() { r := gin.Default() // apply myMiddleware to all routes under /api api := r.Group("/api") api.Use(myMiddleware) // define some routes for the /api group api.GET("/users", getUsers) api.GET("/posts", getPosts) // start the server r.Run(":8080") }
import "github.com/gin-gonic/gin" func main() { r := gin.Default() // apply some middleware to all routes under /api api := r.Group("/api") api.Use(myMiddleware) // apply more middleware to all routes under /api/v1 v1 := api.Group("/v1") v1.Use(myOtherMiddleware) // define some routes for the /api/v1 group v1.GET("/users", getUsers) v1.GET("/posts", getPosts) // start the server r.Run(":8080") }In both examples, the GET method is used to define a route that handles HTTP GET requests. The first argument to the GET method is the path for the route, and the second argument is a function that will be called when the route is matched.