Routing
Echo’s optimized router matches request URLs to handlers using a radix tree with zero dynamic memory allocation and smart route prioritization.
Registering routes
Section titled “Registering routes”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 parametere.POST("/users", createUser)e.PUT("/users/:id", updateUser)e.DELETE("/users/:id", deleteUser)e.GET("/static/*", serveFiles) // wildcardAny 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)Match types
Section titled “Match types”| Pattern | Type | Example match |
|---|---|---|
/users/profile | Static | /users/profile |
/users/:id | Param | /users/42 |
/static/* | Wildcard | /static/css/app.css |
Path parameters
Section titled “Path parameters”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("*"))})Groups
Section titled “Groups”Group routes that share a prefix and middleware with e.Group():
admin := e.Group("/admin", middleware.BasicAuth(authFn))admin.GET("/metrics", metrics) // -> /admin/metricsadmin.GET("/users", listUsers) // -> /admin/usersGroups can be nested to compose larger route trees.