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
javascript
const ROLE_PERMISSIONS = { admin: ['users:read', 'users:write', 'billing:read', 'billing:write'], manager: ['users:read', 'billing:read'], member: ['users:read'],
Role-based permissions middleware in Express
authorization
middleware
rbac
Intermediate
9 steps
go
package handlers import ( "net/http"
Serving embedded static meta files in Gin
embedding
static assets
http caching
Intermediate
6 steps
go
package config import ( "fmt"
A thread-safe config singleton in Go
singleton
concurrency
environment-variables
Intermediate
7 steps
javascript
const express = require('express'); const app = express(); app.get('/health', (req, res) => res.json({ status: 'ok' }));
Graceful shutdown in an Express server
graceful-shutdown
signal-handling
connection-tracking
Advanced
9 steps
php
<?php namespace App\Http\Middleware;
Resolving the current team in Laravel middleware
middleware
multi-tenancy
cookies
Intermediate
8 steps
go
package scheduler import ( "container/heap"
A priority job queue with Go's container/heap
priority-queue
heap
interfaces
Intermediate
9 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.