Skip to content

proposal: use Go 1.27 generic methods for typed Context helpers (PathParam/QueryParam/FormParam/Bind) #3066

Description

@zxysilent

Go 1.27 has been released with support for generic methods (release notes, proposal golang/go#77273). This removes the language limitation mentioned in #2856 — "structs can not have generic methods, only generic functions are allowed" — that forced the typed helpers in v4 to be package-level functions like echo.PathParam[int](c, "id").

Now that v5's Context is a struct and the language supports it, the helpers can become real methods:

Before (v4 style):

id, err := echo.PathParam[int](c, "id")
page, err := echo.QueryParam[int](c, "page")

After (generic methods):

id, err := c.PathParam[int]("id")
page, err := c.QueryParam[int]("page")

Suggested additions (mirroring the v4 helper set):

func (c *Context) PathParam[T any](name string) (T, error)
func (c *Context) QueryParam[T any](name string) (T, error)
func (c *Context) FormParam[T any](name string) (T, error)
func (c *Context) PathParamOr[T any](name string, def T) (T, error)
func (c *Context) QueryParamOr[T any](name string, def T) (T, error)
func (c *Context) FormParamOr[T any](name string, def T) (T, error)
func (c *Context) QueryParams[T any](name string) ([]T, error)
func (c *Context) FormParams[T any](name string) ([]T, error)

Note: since Context is no longer an interface in v5, adding methods is not a breaking change for users — existing code calling c.PathParam("id") keeps compiling, and the existing package-level generic functions can be kept (or deprecated) as thin wrappers.

The only blocker is the minimum Go version: generic methods require go 1.27 in go.mod. Filing this now so it's on the radar for when Echo bumps its minimum supported Go version to 1.27.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions