快速开始
只需几分钟即可启动并运行 Fox。
- Go 1.21 或更高版本
- 对 Go 和 Web 开发有基本了解
go get -u github.com/fox-gonic/fox第一个 Fox 应用
Section titled “第一个 Fox 应用”创建一个新文件 main.go:
package main
import ( "github.com/fox-gonic/fox")
type HelloRequest struct { Name string `form:"name" binding:"required"`}
func main() { // 使用默认中间件创建 Fox 路由器 r := fox.Default()
// 定义一个简单的 GET 端点,自动参数绑定 r.GET("/hello", func(req *HelloRequest) (any, error) { return map[string]string{ "message": "你好," + req.Name + "!", }, nil })
// 定义一个 JSON POST 端点 type CreateUserRequest struct { Name string `json:"name" binding:"required"` Email string `json:"email" binding:"required,email"` }
r.POST("/users", func(req *CreateUserRequest) (any, error) { // 在实际应用中,您会在这里保存到数据库 return map[string]any{ "id": 1, "name": req.Name, "email": req.Email, }, nil })
// 启动服务器 r.Run(":8080")}go run main.go测试 GET 端点:
curl "http://localhost:8080/hello?name=世界"响应:
{ "message": "你好,世界!"}测试 POST 端点:
curl -X POST http://localhost:8080/users \ -H "Content-Type: application/json" \ -d '{"name":"张三","email":"zhangsan@example.com"}'响应:
{ "id": 1, "name": "张三", "email": "zhangsan@example.com"}现在您已经有了一个基本的 Fox 应用在运行,探索更多特性:
r.GET("/user/:id", func(id int) (*User, error) { user, err := db.GetUser(id) if err != nil { return nil, err // Fox 自动处理错误响应 } return user, nil})使用 Gin Context
Section titled “使用 Gin Context”当您需要访问底层 Gin 上下文时:
r.GET("/example", func(c *gin.Context, req *Request) (any, error) { // 直接访问 Gin context userAgent := c.GetHeader("User-Agent")
// 您的逻辑 return response, nil})type SignupRequest struct { Username string `json:"username" binding:"required"` Password string `json:"password" binding:"required"`}
func (r *SignupRequest) IsValid() error { if len(r.Password) < 8 { return errors.New("密码必须至少 8 个字符") } return nil}