Scripts2026年4月12日·1 分钟阅读

Fiber — Express-Inspired Web Framework Written in Go

Fiber is an Express-inspired web framework written in Go, built on top of Fasthttp — the fastest HTTP engine for Go. Familiar Express-like API, zero memory allocation routing, built-in middleware, and WebSocket support.

SC
Script Depot · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

mkdir my-api && cd my-api
go mod init my-api
go get github.com/gofiber/fiber/v2
// main.go
package main

import (
    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/fiber/v2/middleware/cors"
    "github.com/gofiber/fiber/v2/middleware/logger"
)

type Asset struct {
    ID    int    `json:"id"`
    Repo  string `json:"repo"`
    Stars int    `json:"stars"`
}

func main() {
    app := fiber.New()
    app.Use(logger.New())
    app.Use(cors.New())

    assets := []Asset{
        {1, "react", 230000},
        {2, "vue", 210000},
    }

    app.Get("/api/assets", func(c *fiber.Ctx) error {
        return c.JSON(assets)
    })

    app.Post("/api/assets", func(c *fiber.Ctx) error {
        var a Asset
        if err := c.BodyParser(&a); err != nil {
            return c.Status(400).JSON(fiber.Map{"error": err.Error()})
        }
        a.ID = len(assets) + 1
        assets = append(assets, a)
        return c.Status(201).JSON(a)
    })

    app.Listen(":3000")
}
go run main.go
curl http://localhost:3000/api/assets
介绍

Fiber is an Express-inspired web framework written in Go, built on top of Fasthttp — the fastest HTTP engine for Go. Fiber aims to ease things up for fast development with zero memory allocation and performance in mind. Familiar API for developers coming from Express.js or Koa.

What Fiber Does

  • Express-like APIapp.Get(), app.Post(), familiar method signatures
  • Zero memory allocation — routing uses no heap allocations
  • Fasthttp — fastest HTTP library for Go (not net/http)
  • Middleware — Logger, CORS, Rate Limiter, Compress, ETag, Cache, Helmet
  • Template engines — HTML, Pug, Handlebars, Mustache
  • WebSocket — built-in WebSocket support
  • File upload — multipart form handling
  • Static filesapp.Static() built in
  • Group routing — Express-style route groups
  • Hooks — OnRoute, OnName, OnGroup lifecycle hooks

Architecture

Built on Fasthttp (no net/http). Fasthttp reuses request objects and uses byte slices instead of strings to eliminate allocations. Fiber wraps this with an Express-like API. Note: Fasthttp is not fully compatible with Go stdlib middleware (requires adaptation).

Self-Hosting

go build -o server main.go
./server
# Single binary, zero deps

Key Features

  • Fastest Go web framework (Fasthttp-based)
  • Zero allocation routing
  • Express-familiar API
  • 30+ built-in middleware
  • WebSocket support
  • Template engines
  • Route groups
  • Prefork mode for multi-core
  • Graceful shutdown
  • File upload handling

Comparison

Framework HTTP Engine Allocations API Style
Fiber Fasthttp Zero Express
Gin net/http + httprouter Low Express-like
Echo net/http Low Express-like
Chi net/http Low stdlib
stdlib net/http net/http Variable Raw

常见问题 FAQ

Q: Fiber vs Gin? A: Fiber 基于 Fasthttp(更快但不兼容 stdlib);Gin 基于 net/http(兼容 stdlib middleware)。纯性能选 Fiber,最大兼容性选 Gin。

Q: Fasthttp 限制? A: Fasthttp 不支持 HTTP/2、不能直接用 net/http middleware。需要适配器或放弃部分 stdlib 集成。

Q: 适合什么场景? A: 高 QPS 的 REST API、需要最低延迟的微服务、从 Express.js 迁移到 Go。

来源与致谢 Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产