go 49 lines · 6 steps

How route groups nest in Gin

Gin's router groups share a path prefix and middleware, letting you compose versioned, authenticated API trees from small pieces.

Explained by highlit
1package main
2 
3import (
4 "net/http"
5 
6 "github.com/gin-gonic/gin"
7)
8 
9func RegisterRoutes(r *gin.Engine, h *Handler) {
10 api := r.Group("/api")
11 
12 v1 := api.Group("/v1")
13 {
14 v1.Use(AuthRequired())
15 
16 users := v1.Group("/users")
17 {
18 users.GET("", h.ListUsers)
19 users.POST("", h.CreateUser)
20 users.GET("/:id", h.GetUser)
21 users.PATCH("/:id", h.UpdateUser)
22 users.DELETE("/:id", h.DeleteUser)
23 }
24 
25 posts := v1.Group("/posts")
26 {
27 posts.GET("", h.ListPosts)
28 posts.POST("", h.CreatePost)
29 posts.GET("/:id/comments", h.ListComments)
30 }
31 }
32 
33 v2 := api.Group("/v2")
34 {
35 v2.Use(AuthRequired(), RateLimit(100))
36 v2.GET("/users", h.ListUsersV2)
37 v2.POST("/users", h.CreateUserV2)
38 }
39}
40 
41func AuthRequired() gin.HandlerFunc {
42 return func(c *gin.Context) {
43 if c.GetHeader("Authorization") == "" {
44 c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "missing token"})
45 return
46 }
47 c.Next()
48 }
49}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Route groups let a shared prefix and middleware apply to every route registered under them.
  2. 2Nesting groups keeps versioned APIs organized and lets each tier layer on its own middleware.
  3. 3Middleware is just a handler that either aborts the request or calls Next to continue the chain.

Related explainers

Share this explainer

Here's the card — post it anywhere.

How route groups nest in Gin — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code