这是 Fiber v3 一个很实用的点。
官方文档和 What’s New 都明确写了:
你现在可以直接把常见 net/http handler 注册到 Fiber 路由方法里。
例如:
httpHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("served by net/http"))
})
app.Get("/legacy", httpHandler)这对迁移老项目很方便。
但一定要记住官方给的两个限制:
- 这类 handler 走的是兼容层
- 它们不拿到
fiber.Ctx - 也不能用
c.Next() - 性能不如原生 Fiber handler
- 主要适合互操作和过渡期,不适合你把全项目都写成适配态
所以它更像:
快线城区旁边的临时接驳车道。能接老路网,但别把它当整座新区的主跑道。