安装与配置 #
一、环境准备 #
1.1 安装Go语言 #
Fiber需要Go 1.21或更高版本。
Windows安装:
bash
# 下载安装包
# https://go.dev/dl/
# 验证安装
go version
macOS安装:
bash
# 使用Homebrew
brew install go
# 验证安装
go version
Linux安装:
bash
# Ubuntu/Debian
sudo apt update
sudo apt install golang-go
# CentOS/RHEL
sudo yum install golang
# 验证安装
go version
1.2 配置Go环境 #
bash
# 设置GOPATH(可选,Go 1.11+使用Go Modules)
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
# 设置Go代理(国内推荐)
go env -w GOPROXY=https://goproxy.cn,direct
# 验证配置
go env
1.3 配置Go Modules #
bash
# 启用Go Modules
go env -w GO111MODULE=on
# 配置代理
go env -w GOPROXY=https://goproxy.cn,direct
# 配置私有仓库
go env -w GOPRIVATE=github.com/your-org
二、安装Fiber #
2.1 创建项目 #
bash
# 创建项目目录
mkdir my-fiber-app
cd my-fiber-app
# 初始化Go模块
go mod init my-fiber-app
2.2 安装Fiber #
bash
# 安装Fiber v2
go get -u github.com/gofiber/fiber/v2
# 安装常用中间件
go get -u github.com/gofiber/fiber/v2/middleware/logger
go get -u github.com/gofiber/fiber/v2/middleware/recover
go get -u github.com/gofiber/fiber/v2/middleware/cors
2.3 验证安装 #
创建 main.go 文件:
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")
}
运行项目:
bash
go run main.go
访问 http://localhost:3000 验证安装成功。
三、开发工具配置 #
3.1 VS Code配置 #
安装Go扩展:
json
// settings.json
{
"go.useLanguageServer": true,
"go.lintTool": "golangci-lint",
"go.lintOnSave": "package",
"go.formatTool": "goimports",
"editor.formatOnSave": true
}
3.2 GoLand配置 #
- 打开 Settings/Preferences
- Go > GOROOT:设置Go安装路径
- Go > Go Modules:启用Go Modules
- Editor > Code Style > Go:配置代码风格
3.3 推荐扩展 #
| 工具 | 用途 |
|---|---|
| gopls | Go语言服务器 |
| goimports | 自动导入包 |
| golangci-lint | 代码检查 |
| air | 热重载 |
四、项目初始化 #
4.1 基础项目结构 #
bash
my-fiber-app/
├── cmd/
│ └── main.go
├── internal/
│ ├── handlers/
│ ├── models/
│ └── services/
├── pkg/
│ └── utils/
├── config/
│ └── config.go
├── go.mod
├── go.sum
└── .env
4.2 创建基础文件 #
go.mod:
go
module my-fiber-app
go 1.21
require github.com/gofiber/fiber/v2 v2.51.0
main.go:
go
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
)
func main() {
app := fiber.New(fiber.Config{
AppName: "My Fiber App v1.0",
})
// 中间件
app.Use(logger.New())
app.Use(recover.New())
// 路由
app.Get("/", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"message": "Welcome to Fiber!",
})
})
// 启动服务
log.Fatal(app.Listen(":3000"))
}
4.3 环境变量配置 #
安装godotenv:
bash
go get github.com/joho/godotenv
创建 .env 文件:
env
APP_NAME=My Fiber App
APP_PORT=3000
APP_ENV=development
使用环境变量:
go
package main
import (
"log"
"os"
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
)
func main() {
// 加载环境变量
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
app := fiber.New()
port := os.Getenv("APP_PORT")
if port == "" {
port = "3000"
}
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, Fiber!")
})
log.Fatal(app.Listen(":" + port))
}
五、配置详解 #
5.1 Fiber配置选项 #
go
app := fiber.New(fiber.Config{
// 应用名称
AppName: "My App v1.0",
// Prefork模式(多进程)
Prefork: false,
// 严格路由(区分/foo和/foo/)
StrictRouting: false,
// 区分大小写
CaseSensitive: false,
// 不可变请求体
Immutable: false,
// Body限制(默认4MB)
BodyLimit: 4 * 1024 * 1024,
// 读取超时
ReadTimeout: 10 * time.Second,
// 写入超时
WriteTimeout: 10 * time.Second,
// 空闲超时
IdleTimeout: 120 * time.Second,
// 错误处理
ErrorHandler: func(c *fiber.Ctx, err error) error {
return c.Status(500).JSON(fiber.Map{
"error": err.Error(),
})
},
})
5.2 常用配置说明 #
| 配置 | 默认值 | 说明 |
|---|---|---|
| Prefork | false | 启用多进程模式 |
| StrictRouting | false | 严格路由匹配 |
| CaseSensitive | false | 路由大小写敏感 |
| BodyLimit | 4MB | 请求体大小限制 |
| ReadTimeout | 无限制 | 读取超时时间 |
| WriteTimeout | 无限制 | 写入超时时间 |
5.3 Prefork模式 #
Prefork模式可以充分利用多核CPU:
go
app := fiber.New(fiber.Config{
Prefork: true,
})
// 获取当前进程ID
if fiber.IsChild() {
// 子进程
} else {
// 主进程
}
六、热重载配置 #
6.1 使用Air #
安装Air:
bash
go install github.com/cosmtrek/air@latest
创建 .air.toml 配置:
toml
root = "."
tmp_dir = "tmp"
[build]
cmd = "go build -o ./tmp/main ."
bin = "./tmp/main"
full_bin = "./tmp/main"
include_ext = ["go", "tpl", "tmpl", "html"]
exclude_dir = ["assets", "tmp", "vendor"]
delay = 1000
运行:
bash
air
6.2 使用CompileDaemon #
安装:
bash
go install github.com/githubnemo/CompileDaemon@latest
运行:
bash
CompileDaemon -command="./my-fiber-app"
七、常见问题 #
7.1 依赖下载失败 #
bash
# 使用代理
go env -w GOPROXY=https://goproxy.cn,direct
# 清理缓存
go clean -modcache
# 重新下载
go mod download
7.2 版本冲突 #
bash
# 查看依赖树
go mod graph
# 整理依赖
go mod tidy
# 更新依赖
go get -u
7.3 端口占用 #
bash
# 查看端口占用
lsof -i :3000
# 杀掉进程
kill -9 <PID>
八、总结 #
8.1 安装步骤回顾 #
text
1. 安装Go 1.21+
2. 配置Go Modules
3. 创建项目目录
4. 初始化go mod
5. 安装Fiber
6. 创建main.go
7. 运行项目
8.2 下一步 #
环境搭建完成后,让我们创建 第一个应用,开始Fiber开发之旅!
最后更新:2026-03-28