<p>I've seen routers that allows you to use middleware like this:</p>
<pre><code>router.Use(yourMiddleware)
</code></pre>
<p>and i've been thinking that there is another approach and i want to know if it's useful or not, I am practicing on coding a mux in go <a href="https://github.com/emostafa/garson" rel="nofollow">Garson</a> and it occurred to me i could implement middleware usage like this</p>
<pre><code>router := garson.New()
// the next line registers two middlewares to be executed before each request
router.Before(garson.LoggerMiddleware, garson.OnlyJSONMiddleware)
// the next line registers one middleware to be executed after each request
router.After(garson.SomeOtherMiddlware)
// the next line registers one middleware to be executed only before this route
router.Post("/api/comments").Before(AuthMiddleware)
</code></pre>
<p>what do you guys think ? in terms of real projects usage.</p>
<hr/>**评论:**<br/><br/>Vlir: <pre><p>Check out the way Gin does it.. super succint and clear</p></pre>neoasterisk: <pre><p>It looks a little verbose to me and it doesn't seem to provide a good way to chain middleware (in 1 line).</p>
<p>The only way you can be sure about the design is to use your own package to build something with a fair number of routers and middleware.</p>
<p>Personally I never understood why people use external libraries for middleware but I digress.</p></pre>
![](https://static.golangjob.cn/static/img/banner.png)