# 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. ## Install Save the content below to `.claude/skills/` or append to your `CLAUDE.md`: ## Quick Use ```bash mkdir my-api && cd my-api go mod init my-api go get github.com/gin-gonic/gin ``` ```go // main.go 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}, {2, "vue", 210000}, } 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") } ``` ```bash go run main.go curl http://localhost:8080/api/assets ``` ## Intro Gin is the most popular HTTP web framework for Go. It uses httprouter for fast routing and provides a rich middleware system similar to Express (for Node) or Martini (for Go, its predecessor). Gin is designed for building REST APIs, web applications, and microservices at scale. - **Repo**: https://github.com/gin-gonic/gin - **Stars**: 88K+ - **Language**: Go - **License**: MIT ## What Gin Does - **Fast routing** — radix tree via httprouter - **Middleware** — Logger, Recovery, CORS, BasicAuth, custom - **JSON binding** — ShouldBindJSON with validation tags - **Validation** — struct-level validation via go-playground/validator - **Grouping** — route groups with shared middleware - **Error management** — error collection in context - **Rendering** — JSON, XML, YAML, ProtoBuf, HTML templates - **File uploads** — single and multi-file - **Graceful shutdown** — `http.Server.Shutdown()` - **Testing** — httptest integration ## Architecture Radix tree router for O(1) path matching. Context object carries request, response, params, and abort chain. Middleware are handler functions chained via `c.Next()`. Group routers share prefix and middleware. Engine implements `http.Handler` interface. ## Self-Hosting ```bash GIN_MODE=release go build -o server . ./server # Deploy as single binary anywhere ``` ## Key Features - Fastest Go router - Express-like middleware - JSON validation via tags - Route groups - HTML template rendering - File upload handling - Graceful shutdown - Comprehensive logging - Easy testing with httptest ## Comparison | Framework | Router | Middleware | Performance | |---|---|---|---| | Gin | httprouter (radix) | Express-like | Very fast | | Echo | Own (radix) | Express-like | Very fast | | Fiber | Fasthttp | Express-like | Fastest | | Chi | stdlib-compatible | stdlib | Fast | | Gorilla Mux | Regex-based | stdlib | Medium | ## FAQ **Q: Gin vs Echo?** A: Features and performance are nearly identical. Gin has a larger community and the most stars; Echo has better docs and more elegant routing. Up to team preference. **Q: Can it be used with the standard library?** A: Gin implements the `http.Handler` interface and can be embedded in stdlib's `http.Server`. You can also mix stdlib middleware. **Q: For production use?** A: Set `GIN_MODE=release` to disable debug logs. Gin is used in countless production projects (ByteDance, Didi, Qiniu, and other large Chinese tech companies have case studies). ## Sources - Docs: https://gin-gonic.com/docs - GitHub: https://github.com/gin-gonic/gin - License: MIT --- Source: https://tokrepo.com/en/workflows/gin-high-performance-http-web-framework-go-65c0d201 Author: AI Open Source