HackerReposA FIELD GUIDE TO OPEN SOURCE
ProSponsor the shelf♧ 0
← Back to top all-time
gocraft
web
Go2013–2018
gocraft / web

web

Go Router + Middleware. Your Contexts.

4Distinct people
5Mentions
0Explicit recommenders
1.5KGitHub stars
REPOSITORY AT A GLANCE

What to know before exploring

What it is

Go Router + Middleware. Your Contexts.

GitHub

Where it is used

No documented product use has been verified yet.

What people are saying

No recommendation phrase was found in the indexed comments.

No classified warning is attached to this page. Read the discussions before treating silence as approval.

Also in these discussions

No other repository is repeatedly linked in the same discussions.

This week on Hacker News

Nov 7–Nov 14 UTC · Direct repository links and separately labeled related project stories. Indexed through 11/14/2026, 3:56:07 PM UTC.

No direct repository links were found in the indexed HN discussions over the past seven days. Older discussions are available below.

Mentions over time

HN mentions by year5 dated mentions

Observed mentions: 2013-12-08 20:56:03 UTC to 2018-07-14 04:20:18 UTC.

01232013-01-01 00:00:00 UTC to 2014-01-01 00:00:00 UTC (exclusive): 1 mentions; 0 submissions, 1 comments20132014-01-01 00:00:00 UTC to 2015-01-01 00:00:00 UTC (exclusive): 3 mentions; 0 submissions, 3 comments2015-01-01 00:00:00 UTC to 2016-01-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments20152016-01-01 00:00:00 UTC to 2017-01-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2017-01-01 00:00:00 UTC to 2018-01-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2018-01-01 00:00:00 UTC to 2019-01-01 00:00:00 UTC (exclusive): 1 mentions; 0 submissions, 1 comments2018

Counts come from the repository’s dated source evidence. Index cutoff: 2026-11-02 08:16:59 UTC.

View mention counts and date ranges
UTC ranges include their start and exclude their end. Select a period to filter the source evidence.
PeriodStart (UTC)End (exclusive, UTC)MentionsSubmissionsComments
20132013-01-01 00:00:00 UTC2014-01-01 00:00:00 UTC101
20142014-01-01 00:00:00 UTC2015-01-01 00:00:00 UTC303
20152015-01-01 00:00:00 UTC2016-01-01 00:00:00 UTC000
20162016-01-01 00:00:00 UTC2017-01-01 00:00:00 UTC000
20172017-01-01 00:00:00 UTC2018-01-01 00:00:00 UTC000
20182018-01-01 00:00:00 UTC2019-01-01 00:00:00 UTC101

What Hacker News says

5 sources
> Instead of duplicating code to implement common/middleware functionality, I like to just pass around functions. The problem there is that it doesn't allow you to either: • Apply the middleware to routers or sub-routers; • You end up with http.HandleFunc("/", loggingMiddleware(recoveryMiddleware(sessionMiddleware(myHandler)))) - which is no fun, repetitive and prone to errors • There's no request context - your handlers have no way to communicate between themselves (i.e. passing a CSRF token from middleware to the handler rendering the template or writing to the response header) It does work for basic applications, but it just doesn't scale beyond more than a (small) handful of routes. I wrote a little function[1] that helps with avoiding the gratuitous function nesting, but you'll need to BYO request context in many cases. This often en
Read the rest
ds up being map[string]interface{} (so you may want getters/setters to wrap types coming in and out), as the alternative is to rely on structs + methods (ala https://github.com/gocraft/web) which can be a little unwieldy. Horses for courses, as they say! [1]: https://gist.github.com/elithrar/21cb76b8e29398722532
In A Comparison of Go Web Frameworks
I have been using Martini for over a year and we use it on our frontend API which has a non trivial number of endpoints, that contain a lot of code that has little to do with http routing (like I imagine most products do). Yes, Martini is non-idiomatic magic. Specifically codegangsta's 200-line inject (https://github.com/codegangsta/inject) library is most of Martini's magic. I think inject is rather clever (I've begin using it other projects), while non-idiomatic, and is a pretty neat way of handling middlewares - which I think is the most important point. In defense of Martini however I do appreciate that it leaves out boilerplate in many of my functions, and while my functions may look magical (who is calling this?), the routing setup and parameters provide a certain clarity. Concerning Stephen Searles post, I feel #2 & #3 are problems
Read the rest
with the library specifically and not Martini's design. In #2, the default return handler doesn't check if the return value satisfies io.Writer, which it should, but the default return handler can be fixed or swapped by the developer to make sure io.Writer is handled appropriately. #3 can be seen as any issue when your router allows you to specify Regex routes, and with that you have to ensure your routes are valid Regex. If you don't like it, you can specify to not take advantage of it or do whatever the popular Gorrilamux does. Now #1, is a tough one and overlooks what a lot of the other mini-frameworks have been trying to provide and what Stephen's muxchain doesn't provide - how do I manage all the various resources like database connections, config parameters, and the like. How can I write one "User" middleware, and provide the clients login details cleanly to my other middlewares and handlers? muxchain simply makes that the developers problem, so while it claims to provide everything Martini does, I don't really think it does. Gorrila (through Gorilla Context), stretchr's Goweb, Goji and others provide a `map[string]inteface{}.` Okay, so I still lose the type system, but now I have to litter my code with a ton of `realValue, ok := myMap["myValue"].(myType)`. The only simple, type-safe solution I've seen to this problem is Gocraft's web (https://github.com/gocraft/web), which I discovered long after Martini, and is much faster. So far of the frameworks that have been developed because of the whining that Martini has too much magic, only Gocraft has a rather clever way of dealing with this problem. Finally however, I'd imagine in most production applications the router is such a small part of the project I can't really suggest to someone that they should use Python+Twisted for their application if they like Martini. I can understand to a beginner or someone looking to learn Go, Martini may be poisonous, but in real, working, code I've rarely had to even think about Martini, and the rest of my code is fairly idiomatic and type safe. In short, while I do think Negroni is a great project, I think the whole reflection/injection might be a little too blown out of proportion. We will have a better picture for Martini at the end of the day once the Go ecosystem matures but its a little bit premature to say we should stop writing new code with Martini.
In Thoughts on Martini – Introducing Negroni
I agree that 'context' and passing this context between middleware is the biggest missing item from the standard http+"pat". This library (Goji) solves it with a map[string]interface{}. This works, but has the downside that you need to type-assert your values from this map if you want to use them. I wrote a library a while ago (http://www.github.com/gocraft/web) that uses reflection so that your contexts can be strongly typed. Martini offers a similar approach.
In Goji: a web microframework for Go
I believe this is solved with gocraft/web: https://github.com/gocraft/web The middleware would be, per the example there: func (c *YourContext) UserRequired(rw web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) { user := userFromSession(r) // Pretend like this is defined. It reads a session cookie and returns a *User or nil. if user != nil { c.User = user next(rw, r) } else { rw.Header().Set("Location", "/") rw.WriteHeader(http.StatusMovedPermanently) // do NOT call next() } } Compatibility with Handler/HandlerFunc is kept at the top level, but not at every step along the middleware chain.
In Writing HTTP Middleware in Go

MAKE YOURSELF AT HOME

A few shortcuts

/
Search the collection
?
Open this guide
Esc
Close dialogs and navigation

Your saved repositories and interests stay in this browser.