Help us shape the future of Go! Take the 2026 Go Developer Survey

router

package
v0.27.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package router provides Laravel-style declarative routing that is independent of any HTTP framework. You declare routes once; each adapter (Gin/Fiber/Echo/Chi/net/http) mounts them with a one-liner.

Example:

r := router.New()
r.Get("/health", healthHandler)
r.Resource("posts", postsCtrl)
r.Group("/api/v1", func(g *router.Router) {
    g.Use(authMiddleware)
    g.Resource("users", usersCtrl)
})

Then mount onto your framework:

r.MountStd(http.NewServeMux())            // net/http
r.Apply(func(method, path string, h http.HandlerFunc) {
    ginEngine.Handle(method, path, gin.WrapF(h))
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler = http.HandlerFunc

Handler is the framework-agnostic handler signature. It is identical to http.HandlerFunc — controllers generated by `lago make:controller` already conform.

type Middleware

type Middleware func(Handler) Handler

Middleware wraps a Handler. Middleware compose left-to-right.

func Chain

func Chain(mw ...Middleware) Middleware

Chain composes middleware as a single Middleware. Useful when you want to apply a stable bundle (logging + recovery + auth) in one call.

type ResourceController

type ResourceController interface {
	Index(w http.ResponseWriter, r *http.Request)
	Show(w http.ResponseWriter, r *http.Request)
	Store(w http.ResponseWriter, r *http.Request)
	Update(w http.ResponseWriter, r *http.Request)
	Destroy(w http.ResponseWriter, r *http.Request)
}

ResourceController is the contract for `r.Resource("posts", ctrl)`. Implementations may leave any method as a no-op; the router only wires the methods present.

type Route

type Route struct {
	Method  string
	Path    string
	Handler Handler
	Name    string
}

Route is one row in the route table. It is exposed so adapters can introspect (e.g. for printing the route list).

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router collects routes, groups, and middleware in declaration order. It is not goroutine-safe — declare routes from a single goroutine at startup, then mount onto your server.

func New

func New() *Router

New constructs an empty top-level router.

func (*Router) APIResource

func (r *Router) APIResource(name string, ctrl ResourceController)

APIResource is identical to Resource — kept as a convenience alias to match the user-facing Laravel naming convention.

func (*Router) Any

func (r *Router) Any(path string, h Handler)

Any registers the handler for every standard HTTP verb.

func (*Router) Apply

func (r *Router) Apply(register func(method, path string, h Handler))

Apply invokes register for every route. Frameworks register routes via a one-line callback:

r.Apply(func(method, path string, h http.HandlerFunc) {
    ginEngine.Handle(method, path, gin.WrapF(h))
})

func (*Router) Delete

func (r *Router) Delete(path string, h Handler)

func (*Router) Get

func (r *Router) Get(path string, h Handler)

HTTP verb shortcuts. Each registers a route with the router's prefix and applies its middleware chain to the handler.

func (*Router) Group

func (r *Router) Group(prefix string, fn func(g *Router))

Group runs the callback against a child router with the given path prefix. Middleware on the parent applies to the child; new Use() calls inside fn add to the child only.

func (*Router) Head

func (r *Router) Head(path string, h Handler)

func (*Router) MountStd

func (r *Router) MountStd(mux *http.ServeMux)

MountStd registers every route on a net/http ServeMux using the "METHOD /path" syntax introduced in Go 1.22.

func (*Router) Options

func (r *Router) Options(path string, h Handler)

func (*Router) Patch

func (r *Router) Patch(path string, h Handler)

func (*Router) Post

func (r *Router) Post(path string, h Handler)

func (*Router) Put

func (r *Router) Put(path string, h Handler)

func (*Router) Resource

func (r *Router) Resource(name string, ctrl ResourceController)

Resource registers the five canonical RESTful routes for a controller:

GET    /posts            → ctrl.Index
GET    /posts/{id}       → ctrl.Show
POST   /posts            → ctrl.Store
PUT    /posts/{id}       → ctrl.Update
PATCH  /posts/{id}       → ctrl.Update
DELETE /posts/{id}       → ctrl.Destroy

The `name` argument is the plural resource name (`posts`, `users`, …).

func (*Router) Routes

func (r *Router) Routes() []Route

Routes returns a snapshot of every registered route in declaration order. Useful for printing the route table at startup.

func (*Router) Use

func (r *Router) Use(mw ...Middleware)

Use installs middleware that wraps every subsequent handler registered on this router (and its children via Group).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL