router — API 快速参考
router — API 快速参考
Section titled “router — API 快速参考”本页列出 github.com/spcent/plumego/router 导出的每个公开符号。需要边界说明时请读 Router Primer。
路由通常通过 core.App 注册(它内部委托给此包)。需要在 App 生命周期之外使用独立路由器时,才直接使用 router。
import "github.com/spcent/plumego/router"NewRouter
Section titled “NewRouter”func NewRouter() *Router创建并返回新的 Router,路由树为空。
路由注册(Router)
Section titled “路由注册(Router)”AddRoute
Section titled “AddRoute”func (r *Router) AddRoute(method, path string, handler http.Handler, opts ...RouteOption) *RouteGet / Post / Put / Delete / Patch / Any
Section titled “Get / Post / Put / Delete / Patch / Any”func (r *Router) Get(path string, handler http.Handler) *Routefunc (r *Router) Post(path string, handler http.Handler) *Routefunc (r *Router) Put(path string, handler http.Handler) *Routefunc (r *Router) Delete(path string, handler http.Handler) *Routefunc (r *Router) Patch(path string, handler http.Handler) *Routefunc (r *Router) Any(path string, handler http.Handler) *Routefunc (r *Router) Group(prefix string) *Groupfunc (r *Router) Use(middlewares ...func(http.Handler) http.Handler)RouteOption 函数
Section titled “RouteOption 函数”WithRouteName
Section titled “WithRouteName”func WithRouteName(name string) RouteOption为路由分配名称以支持反向解析。
路径参数提取
Section titled “路径参数提取”func Param(r *http.Request, key string) string静态文件服务
Section titled “静态文件服务”Static
Section titled “Static”func (r *Router) Static(prefix, dir string)StaticFS
Section titled “StaticFS”func (r *Router) StaticFS(prefix string, fs http.FileSystem)路径语法参考
Section titled “路径语法参考”| 模式 | 示例 | 匹配规则 |
|---|---|---|
| 静态段 | /api/users | 精确匹配 |
| 命名参数 | /api/users/:id | 单个路径段 |
| 通配符 | /files/*path | 剩余所有段 |