Go Academy · Lesson

Gin Router and Route Groups

Engine setup, groups, and path parameters

Lesson 1 of 413 steps

Gin Router and Route Groups is a free Go Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.

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 absent

Route 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
Free to start

Learn Go with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Gin Router and Route Groups” lesson free?

Yes — any 3 lessons of the Go Academy learning path, including “Gin Router and Route Groups”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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

  1. Gin Router and Route Groups
  2. Request Binding and Validation
  3. Response Helpers and Status Codes
  4. Gin Middleware: Auth and Logging
← Back to Go Academy