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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Route groups let a shared prefix and middleware apply to every route registered under them.
- 2Nesting groups keeps versioned APIs organized and lets each tier layer on its own middleware.
- 3Middleware is just a handler that either aborts the request or calls Next to continue the chain.
Related explainers
rust
use std::time::Duration; use axum::{ http::{header, HeaderValue, Method},
Configuring CORS on an Axum Router
cors
middleware
http-headers
Intermediate
7 steps
go
package config import ( "encoding/json"
Parsing untyped JSON config in Go
json parsing
type assertions
error handling
Intermediate
7 steps
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
javascript
const express = require('express'); const v1 = express.Router();
Versioning an API with Express Routers
api versioning
routing
modularity
Intermediate
10 steps
go
package cache import ( "container/list"
Building a generic LRU cache in Go
lru-cache
generics
linked-list
Intermediate
8 steps
python
import time from collections import defaultdict from threading import Lock
Sliding-window login rate limiting in Flask
rate-limiting
sliding-window
thread-safety
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-route-groups-nest-in-gin-explained-go-2205/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.