Router can recognize two forms of parameters, one is named parameter, the other one is wildcard parameter.
router.Get("/posts/:year/:month/:title", func(c *clevergo.Context) error {
return c.String(http.StatusOK, fmt.Sprintf("title %s, year: %s, month: %s",
c.Params.String("title"),
c.Params.String("year"),
c.Params.String("month"),
))
})
As you can see, :name
is a named parameter. It’s values can be retrieved via Context.Params
.
Named parameters match only one single path segment.
Pattern: /hello/:name
/hello/foo matched
/hello/bar matched
/hello/foo/bar unmatched
The other parameter form is wildcard parameter which have the form *name
. Like the name suggests, they match everything. Therefore, they must always be at the end of the pattern.
router.Get("/static/*filepath", func(c *clevergo.Context) error {
return c.String(http.StatusOK, fmt.Sprintf("filepath: %s", c.Params.String("filepath")))
})
/static/ match
/static/css/app.css match
/static/js/app.js match
All parameters are stored in Context.Params
.
func params(c *clevergo.Context) error {
name := c.Params.String("name")
page, err := c.Params.Int("page")
num, err := c.Params.Int64("num")
amount, err := c.Params.Uint64("amount")
enable, err := c.Params.Bool("enable")
price, err := c.Params.Float64("price")
return err
}
Router.UseRawPath
allows matching parameters that contains escaped slash %2f
.
router.UseRawPath = true