fiber 三步即可启动:fiber.new() 创建应用,app.get() 定义路由,app.listen() 启动服务;无需配置文件、cli 或 go mod init,go 1.21+ 下 go get -u github.com/gofiber/fiber/v2 即可,v2 兼容 1.22/1.23 但已停止更新。

直接用 fiber.New() + app.Get() + app.Listen() 三步就能跑起来,不需要任何配置文件或 CLI 工具。
安装 fiber/v2 是唯一前置动作
确保你用的是 Go 1.21+(v2.x 最后兼容版本),执行:
go get -u github.com/gofiber/fiber/v2
注意:go install 方式不适用——Fiber 没有提供可执行 CLI;go get 是唯一标准安装路径。如果你用的是 Go 1.22 或 1.23,v2 仍可用,但官方已不再为 v2 提供新特性支持;v3 尚未稳定,别在生产环境试。
main.go 里写满 10 行就可访问 localhost:3000
不要新建目录、不用 go mod init 命令(它会自动生成)——直接建一个 main.go,内容如下:
package main
import "github.com/gofiber/fiber/v2"
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, Fiber!")
})
app.Listen(":3000")
}
运行 go run main.go,浏览器打开 http://localhost:3000 即可见响应。关键点:
-
fiber.New()不接受参数,不传 config 也能工作;加 config 反而容易误配超时或 body limit 导致 POST 失败 -
c.SendString()是最轻量的响应方式;别一上来就用c.JSON(),它会触发额外的json.Marshal和 Content-Type 设置 -
app.Listen(":3000")会阻塞主线程,无需log.Fatal包裹——除非你想捕获端口占用错误并退出
常见启动失败原因就三个
刚跑不起来?先看终端输出的错误信息,90% 是以下之一:
-
address already in use:端口被占,换一个,比如app.Listen(":8080") -
cannot find package "github.com/gofiber/fiber/v2":没执行go get,或 GOPROXY 被墙(配国内镜像源再试) -
undefined: fiber.New或cannot use ... (type *fiber.Ctx) as type error:代码里漏了error返回类型,或把c.SendString(...)写成c.SendString(...); return nil—— Fiber 的 handler 签名强制要求返回error
真正卡住的地方往往不是框架本身,而是忘记 handler 函数必须带 error 返回类型,或者顺手把 fiber.Ctx 当成普通 struct 去取字段——它只能通过方法调用,不能直接访问 c.Request 或 c.Response 字段。











