fiber

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Fiber

Fiber

Fiber is an Express.js inspired framework for Go, running on
fasthttp
(the fastest HTTP engine for Go). v3 brings generic support and better middleware.
Fiber是一个受Express.js启发的Go语言框架,运行在
fasthttp
(Go语言最快的HTTP引擎)之上。v3版本带来了泛型支持和更完善的中间件。

When to Use

适用场景

  • Benchmarks: You need the absolute highest raw throughput.
  • Node.js Background: The API (
    app.Get("/", ...)
    feels like Express).
  • Zero Allocation: Obsessive memory optimization.
  • 性能基准测试:你需要极致的原始吞吐量时。
  • 有Node.js背景:其API(如
    app.Get("/", ...)
    )使用起来和Express类似。
  • 零内存分配:对内存优化有极致追求时。

Core Concepts

核心概念

Fasthttp

Fasthttp

Doesn't use
net/http
. Much faster but incompatible with some standard library middlewares.
不使用
net/http
,速度更快但与部分标准库中间件不兼容。

Prefork

预分叉(Prefork)

Spawn multiple processes (SO_REUSEPORT) to utilize all cores.
生成多个进程(基于SO_REUSEPORT)以利用所有CPU核心。

Best Practices (2025)

2025年最佳实践

Do:
  • Use
    v3
    : For stable generic support.
  • Be careful with Prefork: Can cause issues with socket connections if not handled.
  • Use Built-in Middleware: Cache, Compress, CORS are highly optimized.
Don't:
  • Don't hold references:
    fasthttp
    reuses context objects. Copy strings if you need them after handler returns.
建议
  • 使用v3版本:以获得稳定的泛型支持。
  • 谨慎使用Prefork:若处理不当,会导致套接字连接问题。
  • 使用内置中间件:缓存、压缩、CORS等中间件都经过高度优化。
禁忌
  • 不要持有引用
    fasthttp
    会复用上下文对象。如果在处理器返回后还需要字符串,请先复制它们。

References

参考资料