Introduction
Martini is a Go web framework that provides an expressive, Sinatra-like API for handling HTTP requests. It relies on reflection-based dependency injection to wire services into handlers automatically, letting developers write concise route handlers without manual plumbing.
What Martini Does
- Maps HTTP routes to handler functions with automatic parameter injection
- Provides a composable middleware stack for request processing pipelines
- Includes a Classic() preset with logging, recovery, and static file serving
- Supports route groups for organizing related endpoints under shared prefixes
- Injects services into handlers automatically via a reflection-based injector
Architecture Overview
Martini's core is an injector that resolves function parameters at runtime using Go's reflect package. When a request arrives, the router matches it to a handler, the middleware stack runs in sequence, and the injector supplies dependencies (request, response writer, custom services) to each handler. This design keeps handlers short but incurs a small reflection overhead per request.
Self-Hosting & Configuration
- Requires Go 1.18 or later
- Install with
go get github.com/go-martini/martini martini.Classic()bundles Logger, Recovery, and Static middleware- Bind custom services with
m.Map()orm.MapTo()for interface injection - Set the
MARTINI_ENVenvironment variable toproductionto disable debug output
Key Features
- Sinatra-inspired routing with clean, expressive handler signatures
- Dependency injection removes boilerplate wiring of services
- Middleware is composable — add, remove, or reorder with a single call
- Route groups share middleware and path prefixes
- Minimal core with optional add-on packages for sessions, rendering, and auth
Comparison with Similar Tools
- Gin — uses httprouter for speed and avoids reflection; Martini trades performance for expressiveness
- Echo — similar middleware model but without reflection-based DI
- Negroni — middleware-only library by the same author; Martini adds routing and DI on top
- Revel — full MVC framework; Martini is a lighter, library-style approach
- Fiber — fasthttp-based for throughput; Martini targets developer ergonomics
FAQ
Q: Is Martini still maintained? A: The repository is not archived but receives infrequent updates. It remains usable and stable for existing projects.
Q: Does reflection hurt performance? A: There is a measurable overhead per request compared to non-reflective routers like httprouter, but for most workloads it is not the bottleneck.
Q: Can I use Martini with Go modules?
A: Yes. The github.com/go-martini/martini import path works with Go modules.
Q: What is the relationship between Martini and Negroni? A: Both were created by the same developer. Negroni focuses solely on middleware composition, while Martini adds routing and dependency injection.