跳转到内容

router — API 快速参考

本页列出 github.com/spcent/plumego/router 导出的每个公开符号。需要边界说明时请读 Router Primer

路由通常通过 core.App 注册(它内部委托给此包)。需要在 App 生命周期之外使用独立路由器时,才直接使用 router

import "github.com/spcent/plumego/router"

func NewRouter() *Router

创建并返回新的 Router,路由树为空。


func (r *Router) AddRoute(method, path string, handler http.Handler, opts ...RouteOption) *Route
func (r *Router) Get(path string, handler http.Handler) *Route
func (r *Router) Post(path string, handler http.Handler) *Route
func (r *Router) Put(path string, handler http.Handler) *Route
func (r *Router) Delete(path string, handler http.Handler) *Route
func (r *Router) Patch(path string, handler http.Handler) *Route
func (r *Router) Any(path string, handler http.Handler) *Route
func (r *Router) Group(prefix string) *Group
func (r *Router) Use(middlewares ...func(http.Handler) http.Handler)

func WithRouteName(name string) RouteOption

为路由分配名称以支持反向解析。


func Param(r *http.Request, key string) string

func (r *Router) Static(prefix, dir string)
func (r *Router) StaticFS(prefix string, fs http.FileSystem)

模式示例匹配规则
静态段/api/users精确匹配
命名参数/api/users/:id单个路径段
通配符/files/*path剩余所有段