Favicon of gin

gin

A Go web framework with a zero-allocation router, built-in middleware, and Express-style routing for REST APIs and microservices.

Gin is an HTTP web framework for Go, built around a zero-allocation router based on httprouter and designed for building REST APIs, web applications, and microservices where response speed and low overhead matter. It offers an Express.js-style routing API on top of Go's performance characteristics, along with built-in JSON binding and validation, route grouping, and crash recovery middleware. It's aimed at Go developers who want a lightweight, fast framework for HTTP services without a lot of boilerplate.

Key features

  • Zero-allocation router: routes requests with no heap allocations, which the README credits as the main reason Gin outperforms many other Go routing libraries in benchmarks.
  • Middleware system: an extensible middleware chain for concerns like authentication, logging, and CORS, with gin.Default() including logging and panic-recovery middleware out of the box.
  • Crash-free recovery: built-in recovery middleware catches panics so a single bad request doesn't take down the whole server.
  • JSON binding and validation: automatic binding and validation of request and response JSON against your Go structs.
  • Route grouping: organize related routes and apply shared middleware to a group instead of repeating it per route.
  • Built-in rendering: renders JSON, XML, and HTML templates, among other response formats, without pulling in a separate rendering library.
  • Large middleware ecosystem: the official gin-contrib collection covers JWT and session auth, CORS, rate limiting, compression, logging, metrics, tracing, static file serving, and template engines, alongside community middleware in gin-gonic/contrib.
  • Documented performance: the README publishes its own GitHub API routing benchmark comparing Gin against dozens of other Go routers and frameworks, with detailed benchmark results in a separate BENCHMARKS.md file.

Ideal use cases

Gin fits Go developers building REST APIs, microservices, or backend web services that need to handle a high volume of concurrent requests with minimal per-request overhead. It's a reasonable default when you want more structure and convenience than Go's standard net/http package alone, routing, middleware, binding, rendering, without adopting a large, opinionated full-stack framework. Its production usage list in the README, a push notification server, a serverless platform, a photo management app, an API gateway, an image processing server, and a job scheduler, points at API-heavy backend services as its natural home.

It's a weaker fit if you're not writing Go, obviously, or if you need a more full-stack, batteries-included framework with things like an ORM, admin panel, or asset pipeline built in. Gin intentionally stays focused on the HTTP layer, so you'll be picking your own libraries for database access, background jobs, and anything beyond routing, middleware, and rendering. Teams that are performance-sensitive but don't want to write against net/http directly are a good fit too, since Gin's own benchmark numbers show materially fewer allocations per request than several comparable Go frameworks in the README's GitHub API routing test.

Installation

Gin requires Go 1.25 or above. With Go's module support, import it directly and Go fetches it automatically during build:

import "github.com/gin-gonic/gin"

A minimal application:

package main

import (
  "log"
  "net/http"

  "github.com/gin-gonic/gin"
)

func main() {
  r := gin.Default()

  r.GET("/ping", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{
      "message": "pong",
    })
  })

  if err := r.Run(); err != nil {
    log.Fatalf("failed to run server: %v", err)
  }
}

Save this as main.go and run it:

go run main.go

Then visit http://localhost:8080/ping in a browser or with curl; you should see {"message":"pong"}. By default the server listens on 0.0.0.0:8080 (localhost:8080 on Windows). From here, the project's example repository (gin-gonic/examples) covers REST APIs, authentication and middleware, file uploads, WebSockets, and template rendering in more depth, and the full documentation is available at gin-gonic.com in multiple languages, including English, Simplified and Traditional Chinese, Japanese, Korean, Spanish, Turkish, Persian, Portuguese, Russian, and Indonesian. There's also an official Go.dev tutorial walking through building a RESTful API with Go and Gin from scratch.

Categories:

Frequently asked questions

Share:

Stars
88.9K
Forks
8.6K
Last commit
13 days ago
Repository age
12 years
License
MIT
Self-hosted
No
Activity score
89/100
View Repository
Built with:

Similar to gin

Favicon

 

  
  
Favicon

 

  
  
Favicon