Skip to content

Module 3: Gin Framework (The Express Delivery)

๐Ÿ“š Module 3: The Gin Framework

Course ID: GO-203
Subject: The Express Delivery

While the standard library is great, most professional developers use Gin. It adds โ€œHelperโ€ tools that make building APIs much faster.


๐Ÿ—๏ธ Step 1: Why use a Framework?

๐Ÿงฉ The Analogy: The Hand-Crafted vs. The Assembly Line

  • net/http (Standard Lib): You build every part of your car by hand. Itโ€™s beautiful and fast, but it takes time.
  • Gin: You use a factory assembly line. It handles common things (like reading IDs from URLs or checking for errors) automatically.

๐Ÿ—๏ธ Step 2: In Code

func main() {
    // 1. Start the factory
    r := gin.Default()

    // 2. Simple Routing
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })

    // 3. Start the delivery line
    r.Run(":8080")
}

๐Ÿ—๏ธ Step 3: Grouping & Middleware

Gin allows you to group URLs (e.g., all URLs starting with /api/v1) and apply Middleware (like a security guard) to all of them at once.


๐Ÿฅ… Module 3 Review

  1. Gin: A lightweight, high-performance web framework.
  2. Context: The object that holds all information about the request and response.
  3. gin.H: A shortcut for writing JSON maps.

:::tip Senior Tip Goโ€™s frameworks are โ€œMicro.โ€ Unlike Django or Rails, Gin doesnโ€™t force you to use a specific database or folder structure. You are still the boss! ::: Riverside. Riverside.