Optional Parameter Example
This project demonstrates how to handle optional parameters in a Go application using the Fiber framework.
Prerequisites
Ensure you have the following installed:
- Golang
- Fiber package
Setup
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git
cd recipes/optional-parameter -
Install dependencies:
go get
Running the Application
- Start the application:
go run main.go
Example
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"
.