Created
May 31, 2022 05:46
-
-
Save code1x1/5291131b4be788e64a866b2bf45c16d8 to your computer and use it in GitHub Desktop.
Go fiber pass objects between handlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/gofiber/fiber/v2" | |
) | |
func main() { | |
app := fiber.New() | |
app.Use("/my-middleware", func(c *fiber.Ctx) error { | |
c.Context().SetUserValue("my-key", struct { | |
name, address, email string | |
}{ | |
"john doe", | |
"star valley 1200", | |
"john.doe@star-valley.net", | |
}) | |
return c.Next() | |
}) | |
app.Get("/my-middleware/my-handler", func(c *fiber.Ctx) error { | |
myKey := c.Context().UserValue("my-key").(struct { | |
name, address, email string | |
}) | |
return c.JSON(fiber.Map{ | |
"name": myKey.name, | |
"address": myKey.address, | |
"email": myKey.email, | |
}) | |
}) | |
panic(app.Listen(":8080")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment