With()

Negroni 还有一个便利的函数叫 With. With 函数可以把一个或多个 Handler 实例和接收者处理器集合组合成新的处理器集合,并返回新的 Negroni 实例对象。

  1. // middleware we want to reuse
  2. common := negroni.New()
  3. common.Use(MyMiddleware1)
  4. common.Use(MyMiddleware2)
  5.  
  6. // `specific` is a new negroni with the handlers from `common` combined with the
  7. // the handlers passed in
  8. specific := common.With(
  9. SpecificMiddleware1,
  10. SpecificMiddleware2
  11. )