| title | Optional Parameter | ||
|---|---|---|---|
| keywords |
|
||
| description | Handling optional parameters. |
This project demonstrates how to handle optional parameters in a Go application using the Fiber framework.
Ensure you have the following installed:
- Golang
- Fiber package
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/optional-parameter -
Install dependencies:
go get
- Start the application:
go run main.go
Here is an example of how to handle optional parameters in a Fiber application:
package main
import (
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
app.Get("/user/:id?", func(c fiber.Ctx) error {
id := c.Params("id", "defaultID")
return c.SendString("User ID: " + id)
})
app.Listen(":3000")
}In this example:
- The
:id?parameter in the route is optional. - If the
idparameter is not provided, it defaults to"defaultID".