Gin — High-Performance HTTP Web Framework for Go
Gin is a high-performance HTTP web framework written in Go. Provides a Martini-like API but with significantly better performance — up to 40 times faster thanks to httprouter. The most popular Go web framework for REST APIs and microservices.
Installation avec revue préalable
Cet actif nécessite une revue. Le prompt copié demande un dry-run, affiche les écritures, puis continue seulement après confirmation.
npx -y tokrepo@latest install 65c0d201-3630-11f1-9bc6-00163e2b0d79 --target codexDry-run d'abord, confirmez les écritures, puis lancez cette commande.
What it is
Gin is the most widely adopted HTTP web framework for Go. It wraps httprouter with a developer-friendly API inspired by Martini, delivering structured routing, middleware chaining, and built-in JSON binding. The project targets backend engineers building REST APIs, microservices, and web applications in Go who need performance without sacrificing ergonomics.
If you write Go services that handle HTTP traffic, Gin is the de-facto starting point. It handles routing, request validation, error recovery, and response serialization out of the box.
How it saves time or tokens
Gin eliminates boilerplate that Go developers typically write by hand: route grouping, parameter parsing, input validation via struct tags, and panic recovery middleware. A complete CRUD API that would take 200+ lines with net/http compresses to under 60 lines with Gin. The radix-tree router also means route matching is O(1) in practice, so your API latency stays low even with hundreds of endpoints.
For AI-assisted development, Gin's declarative struct binding means an LLM can generate a working endpoint from a single struct definition, reducing token consumption when scaffolding APIs.
How to use
- Initialize a Go module and install Gin:
mkdir my-api && cd my-api
go mod init my-api
go get github.com/gin-gonic/gin
- Create
main.gowith your routes and handlers:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
type Asset struct {
ID int `json:"id"`
Repo string `json:"repo" binding:"required"`
Stars int `json:"stars" binding:"gte=0"`
}
var assets = []Asset{{1, "react", 230000}}
func main() {
r := gin.Default()
r.GET("/api/assets", func(c *gin.Context) {
c.JSON(http.StatusOK, assets)
})
r.POST("/api/assets", func(c *gin.Context) {
var a Asset
if err := c.ShouldBindJSON(&a); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
a.ID = len(assets) + 1
assets = append(assets, a)
c.JSON(http.StatusCreated, a)
})
r.Run(":8080")
}
- Run and test:
go run main.go
curl http://localhost:8080/api/assets
Example
A grouped route with authentication middleware:
auth := r.Group("/admin")
auth.Use(AuthRequired())
{
auth.GET("/dashboard", dashboardHandler)
auth.POST("/users", createUserHandler)
}
This pattern keeps public and protected routes cleanly separated while reusing middleware across the group.
Related on TokRepo
- Coding tools and frameworks -- Explore more developer frameworks indexed on TokRepo
- Automation tools -- CLI and build tools that pair well with Gin services
Common pitfalls
- Using
c.Bind()instead ofc.ShouldBind()causes Gin to abort on validation errors with a 400 status, which may surprise you when you want custom error responses. - Forgetting to call
c.Next()inside middleware blocks the handler chain silently. - Gin's
Default()includes Logger and Recovery middleware; in production, replace them with structured logging (e.g., zerolog) for better observability. - Sharing mutable state across handlers without a mutex leads to race conditions under concurrent requests.
- Not setting
gin.SetMode(gin.ReleaseMode)in production leaves debug output enabled, leaking internal details.
Questions fréquentes
Gin builds on top of net/http but adds routing via httprouter, middleware chaining, request binding with validation tags, and structured error handling. You get the same underlying HTTP server with a much smaller boilerplate surface. For simple single-endpoint services net/http suffices, but for multi-route APIs Gin saves significant development time.
Gin itself focuses on HTTP request-response cycles. For WebSocket support, you integrate the gorilla/websocket or nhooyr/websocket library and upgrade the connection inside a Gin handler. The Gin context passes through normally, so you keep middleware benefits like auth checks before the upgrade.
Gin claims up to 40x better performance than Martini. The difference comes from httprouter's radix-tree algorithm versus Martini's reflection-based dependency injection. In real-world benchmarks, Gin handles tens of thousands of requests per second on modest hardware with minimal memory allocation per request.
Yes. Since Go 1.8, you can wrap Gin's engine in an http.Server and call srv.Shutdown(ctx) on receiving SIGINT or SIGTERM. The Gin documentation provides a complete graceful-shutdown example using signal.NotifyContext. This ensures in-flight requests finish before the process exits.
Use the official gin-contrib/cors middleware. Import it, configure allowed origins, methods, and headers, then apply it with r.Use(cors.New(config)). This avoids hand-rolling CORS headers and handles preflight OPTIONS requests automatically.
Sources citées (3)
- Gin GitHub— Gin uses httprouter for radix-tree based routing
- Gin README— Up to 40x faster than Martini
- httprouter GitHub— httprouter uses a radix tree for route matching
En lien sur TokRepo
Fil de discussion
Actifs similaires
Echo — High Performance Minimalist Go Web Framework
Echo is a high performance, minimalist Go web framework. Clean API, automatic TLS, HTTP/2, data binding, middleware, and group routing. A strong alternative to Gin with excellent documentation and built-in features.
gRPC-Go — High-Performance RPC Framework for Go
gRPC-Go is the Go implementation of gRPC, a high-performance, open-source RPC framework. It uses Protocol Buffers for serialization and HTTP/2 for transport, enabling efficient communication between microservices with strongly-typed contracts.
Varnish Cache — High-Performance HTTP Reverse Proxy and Accelerator
An open-source HTTP reverse proxy and caching engine designed to accelerate web applications by serving content from memory at high throughput.
Apache Dubbo — High-Performance Java RPC Framework
A guide to Apache Dubbo, the high-performance RPC framework for building scalable microservices with service discovery, load balancing, and traffic management.