title | keywords | description | ||
---|---|---|---|---|
Optional Parameter |
|
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/v2"
)
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
id
parameter is not provided, it defaults to"defaultID"
.