首頁  >  文章  >  後端開發  >  Fiber 中的哪一個函數讓邏輯類似 gin 中的 func (c *Context) Set(key string, value any) ?

Fiber 中的哪一個函數讓邏輯類似 gin 中的 func (c *Context) Set(key string, value any) ?

WBOY
WBOY轉載
2024-02-06 09:25:04740瀏覽

Fiber 中的哪个函数使逻辑类似于 gin 中的 func (c *Context) Set(key string, value any) ?

問題內容

Fiber 中的哪個函數讓邏輯類似 gin 中的 func (c *Context) Set(key string, value any)?

我正在尋找一個在特定上下文中編寫鍵/值對的函數,但我沒有找到它,請告訴我是否有這樣的機會


#正確答案


#看起來(*ctx).locals 就是你想要的。

以下內容複製自官方文件

當地人

一種儲存作用域為請求的變數的方法,因此僅可用於與請求相符的路由。

簽名

func (c *ctx) locals(key interface{}, value ...interface{}) interface{}

範例

app.Use(func(c *fiber.Ctx) error {
  c.Locals("user", "admin")
  return c.Next()
})

app.Get("/admin", func(c *fiber.Ctx) error {
  if c.Locals("user") == "admin" {
    return c.Status(fiber.StatusOK).SendString("Welcome, admin!")
  }
  return c.SendStatus(fiber.StatusForbidden)
})

以上是Fiber 中的哪一個函數讓邏輯類似 gin 中的 func (c *Context) Set(key string, value any) ?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除