Gin Router and Route Groups
Engine setup, groups, and path parameters
Gin Router and Route Groups is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is Gin?
Gin is a high-performance HTTP web framework for Go. It uses a radix tree router, provides middleware support, JSON binding, and validation — making it popular for REST APIs.
Installing and basic setup
Import and create a Gin engine:
go get github.com/gin-gonic/gin
package main
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "pong"})
})
r.Run(":8080")
}gin.Default vs gin.New
gin.Default() includes Logger and Recovery middleware. gin.New() creates an engine with no middleware — use it when you want full control over middleware.
Route methods
Register routes for HTTP methods: GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD, and Any:
r.GET("/users", listUsers)
r.POST("/users", createUser)
r.PUT("/users/:id", updateUser)
r.DELETE("/users/:id", deleteUser)Path parameters
Extract path parameters with c.Param("name"):
r.GET("/users/:id", func(c *gin.Context) {
id := c.Param("id")
c.JSON(200, gin.H{"id": id})
})Query parameters
Read query parameters with c.Query and c.DefaultQuery:
page := c.DefaultQuery("page", "1")
limit := c.Query("limit") // "" if absentRoute groups
Group related routes under a common prefix with r.Group. Middleware applied to the group affects all routes in it:
api := r.Group("/api/v1")
{
api.GET("/users", listUsers)
api.POST("/users", createUser)
api.PUT("/users/:id", updateUser)
}Nested groups
Groups can be nested for fine-grained organisation:
admin := r.Group("/admin")
admin.Use(AdminAuth())
{
admin.GET("/stats", getStats)
admin.DELETE("/users/:id", deleteUser)
}No route / method not allowed
Handle 404 and 405 gracefully with r.NoRoute and r.NoMethod:
r.NoRoute(func(c *gin.Context) {
c.JSON(404, gin.H{"error": "not found"})
})Static files
Serve static files from a directory:
r.Static("/static", "./public")
r.StaticFile("/favicon.ico", "./favicon.ico")Engine as http.Handler
Gin's *gin.Engine implements http.Handler, so you can use it with a custom http.Server for timeout configuration:
srv := &http.Server{Addr: ":8080", Handler: r, ReadTimeout: 5*time.Second}
srv.ListenAndServe()Quick Check
What is the advantage of using route groups in Gin?
Recap: Gin Router and Groups
Key points:
- gin.Default() includes Logger+Recovery; gin.New() for custom setup
- c.Param for path params; c.Query for query params
- r.Group("/prefix") for shared prefix and middleware
- Gin implements http.Handler — use with custom http.Server
Frequently asked questions
Is the “Gin Router and Route Groups” lesson free?
Yes — the full text of “Gin Router and Route Groups” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Gin Router and Route Groups”?
Engine setup, groups, and path parameters You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Gin Router and Route Groups” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Gin Router and Route Groups
- Request Binding and Validation
- Response Helpers and Status Codes
- Gin Middleware: Auth and Logging