From 6c9510df5a376c9a9dd2fbc7310f032e9137d24f Mon Sep 17 00:00:00 2001 From: Jason McNeil Date: Tue, 11 Jun 2024 03:53:43 -0300 Subject: [PATCH] docs: Improve ctx.Locals method description and example (#3030) --- docs/api/ctx.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/api/ctx.md b/docs/api/ctx.md index 52f1a3ad..fdb2adc0 100644 --- a/docs/api/ctx.md +++ b/docs/api/ctx.md @@ -900,10 +900,10 @@ app.Get("/", func(c *fiber.Ctx) error { ## Locals -A method that stores variables scoped to the request and, therefore, are available only to the routes that match the request. +A method that stores variables scoped to the request and, therefore, are available only to the routes that match the request. The stored variables are removed after the request is handled. If any of the stored data implements the `io.Closer` interface, its `Close` method will be called before it's removed. :::tip -This is useful if you want to pass some **specific** data to the next middleware. +This is useful if you want to pass some **specific** data to the next middleware. Remember to perform type assertions when retrieving the data to ensure it is of the expected type. You can also use a non-exported type as a key to avoid collisions. ::: ```go title="Signature" @@ -911,17 +911,20 @@ func (c *Ctx) Locals(key interface{}, value ...interface{}) interface{} ``` ```go title="Example" +type keyType struct{} +var userKey keyType + app.Use(func(c *fiber.Ctx) error { - c.Locals("user", "admin") + c.Locals(userKey, "admin") // Stores the string "admin" under a non-exported type key return c.Next() }) app.Get("/admin", func(c *fiber.Ctx) error { - if c.Locals("user") == "admin" { + user, ok := c.Locals(userKey).(string) // Retrieves the data stored under the key and performs a type assertion + if ok && user == "admin" { return c.Status(fiber.StatusOK).SendString("Welcome, admin!") } return c.SendStatus(fiber.StatusForbidden) - }) ```