Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Mastering Gin: A Hands‑On Guide to Building High‑Performance Go APIs

Mastering Gin: A Hands‑On Guide to Building High‑Performance Go APIs

This tutorial introduces the Gin web framework for Go, covering its core features, installation steps, a simple "Hello World" example, request and response handling, parameter binding, route groups, middleware patterns, and useful utilities for building fast RESTful services.

The Gin framework is a high‑performance web library written in Go, known for its speed, simplicity, and lightweight HTTP router with middleware support. It is widely used to build RESTful APIs and web services by minimizing configuration and boilerplate code.

Installation and Setup

Ensure a Go development environment is installed, then add Gin with the command: go get -u github.com/gin-gonic/gin After installation, you can start coding immediately.

Simple Hello World Example

package main

import (
    "github.com/gin-gonic/gin"
    "net/http"
)

func main() {
    // Create a default router with Logger and Recovery middleware
    r := gin.Default()
    // Register a GET route for the root path
    r.GET("/", func(c *gin.Context) {
        // Respond with a plain string and HTTP 200 status
        c.String(http.StatusOK, "Hello FunTester")
    })
    // Listen on port 8000
    r.Run(":8000")
}

This example demonstrates the basic workflow: create a router, define a handler, and start the server.

Request and Response Basics

The GET method signature is func (c *gin.Context), where gin.Context is the core structure that provides access to the request, response, parameters, and middleware chain.

Supported HTTP Methods

GET : retrieve resources

POST : create resources

PUT : update resources

DELETE : delete resources

PATCH : partially update resources

HEAD : fetch response headers only

OPTIONS : list supported HTTP methods

TRACE : echo the received request for diagnostics

Response Helpers

// JSON response
router.GET("/json", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{"message": "hello", "status": "success"})
})

// String response
router.GET("/string", func(c *gin.Context) {
    c.String(http.StatusOK, "Hello, world")
})

// HTML response
router.LoadHTMLGlob("templates/*")
router.GET("/index", func(c *gin.Context) {
    c.HTML(http.StatusOK, "index.tmpl", gin.H{"title": "Main website"})
})

Parameter Parsing

Path Parameters

router.GET("/user/:id/*path", func(c *gin.Context) {
    id := c.Param("id")
    path := c.Param("path")
    c.String(http.StatusOK, "User ID: %s Path: %s", id, path)
})

In the example, id captures the segment after /user/, while path captures the remaining wildcard part.

Query Parameters

c.Query(key)

– returns the value as a string. c.DefaultQuery(key, defaultValue) – returns a default if the key is missing. c.GetQuery(key) – returns the value and a boolean indicating presence.

func Handler(c *gin.Context) {
    name := c.Query("name")
    name = c.DefaultQuery("name", "tizi365")
    id, ok := c.GetQuery("id")
    if !ok {
        // handle missing parameter
    }
}

POST Form Parameters

c.PostForm(key)
c.DefaultPostForm(key, defaultValue)
c.GetPostForm(key)
func Handler(c *gin.Context) {
    name := c.PostForm("name")
    name = c.DefaultPostForm("name", "tizi365")
    id, ok := c.GetPostForm("id")
    if !ok {
        // handle missing parameter
    }
}

Binding to Structs

Gin can automatically bind JSON, XML, or form data to a Go struct.

type User struct {
    Name string `json:"name" form:"name"`
    Age  int    `json:"age"  form:"age"`
}

router.POST("/login", func(c *gin.Context) {
    u := &User{}
    if err := c.ShouldBind(u); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error"})
        return
    }
    c.JSON(http.StatusOK, u)
})

Route Groups

Group routes to share a common prefix and middleware:

user := r.Group("/user") {
    user.GET("/login", func(c *gin.Context) {
        // handler code
    })
}

Middleware

Middleware functions run before or after request handling. They can be applied globally, to a route group, or to a single route.

Logging Middleware Example

func Logger() gin.HandlerFunc {
    return func(c *gin.Context) {
        start := time.Now()
        c.Next()
        latency := time.Since(start)
        log.Printf("Latency: %v", latency)
        status := c.Writer.Status()
        log.Printf("Status: %d", status)
    }
}

Applying the middleware:

Global: router.Use(Logger()) Group: apiGroup.Use(Logger()) Single route:

router.GET("/admin", Logger(), handler)

Response‑Capturing Middleware Example

func Check() gin.HandlerFunc {
    return func(c *gin.Context) {
        c.Set("example", "12345")
        w := &responseWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
        c.Writer = w
        c.Next()
        responseBody := w.body.String()
        logger.Info("response", zap.String("response", responseBody))
    }
}

type responseWriter struct {
    gin.ResponseWriter
    body *bytes.Buffer
}

func (w responseWriter) Write(b []byte) (int, error) {
    w.body.Write(b)
    return w.ResponseWriter.Write(b)
}

Additional Utilities

Get client IP: c.ClientIP() Set mode: gin.SetMode(gin.ReleaseMode) or gin.SetMode(gin.DebugMode) Gin’s Release mode reduces logging and enables optimizations for production, while Debug mode provides detailed logs for development.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
middlewareGoroutingWeb FrameworkRESTful APIGin

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion