Gin is a high-performance HTTP web framework written in Go that delivers up to 40 times faster performance than Martini, thanks to its radix tree-based routing engine built on top of httprouter. It provides a Martini-like API with essential features like middleware support, JSON binding and validation, and flexible rendering, making it ideal for building REST APIs, microservices, and web applications where speed and developer productivity are paramount. A key insight: Gin's strength lies in its minimal abstraction over Go's net/http, allowing you to leverage Go's concurrency model directly while gaining productivity through built-in helpers—understand that gin.Context extends context.Context and manages both request lifecycle and response rendering, so copying it correctly for goroutines is essential to avoid context cancellation issues.
What This Cheat Sheet Covers
This topic spans 25 focused tables and 167 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Basic Setup and Configuration
The bare essentials to get a Gin server breathing: install the package, create an engine, and call Run. The one distinction worth internalizing early is gin.Default() versus gin.New()—the former hands you Logger and Recovery middleware out of the box, the latter gives you a clean slate. Setting the mode and trusted proxies belongs here too, since both matter the moment you move toward production.
| Action | Example | Description |
|---|---|---|
go get -u github.com/gin-gonic/gin | Downloads and installs the Gin framework into your Go module. | |
r := gin.Default() | • Creates a new Gin router with Logger and Recovery middleware pre-installed • use gin.New() for a bare engine without defaults | |
r.Run(":8080") | • Starts the HTTP server on the specified port • omit port to use PORT environment variable or default :8080. |