Skip to content

Routing

Echo’s optimized router matches request URLs to handlers using a radix tree with zero dynamic memory allocation and smart route prioritization.

Use the HTTP-method helpers on the Echo instance. Each takes a path pattern and a HandlerFunc (func(c *echo.Context) error), with optional route-level middleware.

e := echo.New()
e.GET("/users/:id", getUser) // named parameter
e.POST("/users", createUser)
e.PUT("/users/:id", updateUser)
e.DELETE("/users/:id", deleteUser)
e.GET("/static/*", serveFiles) // wildcard

Any registers a handler for all supported methods, and Match for a specific set:

e.Any("/ping", pong)
e.Match([]string{http.MethodGet, http.MethodPost}, "/form", handleForm)
PatternTypeExample match
/users/profileStatic/users/profile
/users/:idParam/users/42
/static/*Wildcard/static/css/app.css

Read named parameters from the context with c.Param() (or c.ParamOr() for a default):

func getUser(c *echo.Context) error {
id := c.Param("id")
return c.String(http.StatusOK, id)
}

The wildcard segment is available as the * parameter:

e.GET("/files/*", func(c *echo.Context) error {
return c.String(http.StatusOK, c.Param("*"))
})

Group routes that share a prefix and middleware with e.Group():

admin := e.Group("/admin", middleware.BasicAuth(authFn))
admin.GET("/metrics", metrics) // -> /admin/metrics
admin.GET("/users", listUsers) // -> /admin/users

Groups can be nested to compose larger route trees.