Skip to content

Instantly share code, notes, and snippets.

@code1x1
Created May 31, 2022 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code1x1/5291131b4be788e64a866b2bf45c16d8 to your computer and use it in GitHub Desktop.
Save code1x1/5291131b4be788e64a866b2bf45c16d8 to your computer and use it in GitHub Desktop.
Go fiber pass objects between handlers
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