$ show gin-gonic/examples
This repository is a collection of standalone example applications demonstrating features of the Gin web framework for Go. It includes over 30 examples covering routing, middleware, authentication, file uploads, WebSocket, server-sent events, gRPC integration, OpenTelemetry, and more. Each example is a self-contained Go program with its own main.go and dependencies. The codebase serves as a learning resource and reference for Gin patterns, but is not a cohesive production system. Many examples are simple and straightforward, though some demonstrate non-trivial patterns like rate limiting, struct-level validation, and concurrent room management.
The LLM identified 4 files worth highlighting for reuse:
Implements a rate-limiting middleware using a leaky bucket algorithm via the go.uber.org/ratelimit library. It accepts a configurable requests-per-second (RPS) flag and logs the time between requests. The middleware is self-contained and can be extracted into any Gin project for request rate limiting.
Demonstrates how to register a struct-level validation function with the go-playground/validator library within a Gin application. It validates that either FirstName or LastName is present, showing cross-field validation that would be cumbersome with field-level tags alone. This pattern is highly reusable for complex validation logic.
Implements a goroutine-safe room manager using Go channels and the go-broadcast library. The Manager struct handles registering/deregistering listeners, broadcasting messages, and cleaning up rooms. This pattern is a clean, reusable foundation for any real-time pub-sub system (chat, notifications, etc.) and demonstrates advanced channel-based concurrency in Go.
Provides a robust example of limiting file upload size using http.MaxBytesReader. It returns custom JSON error messages for oversized files (413) and missing files (400). The handler is well-commented, includes unit/integration tests, and the pattern can be directly reused in any Gin application requiring strict file size enforcement.