diff --git a/context.go b/context.go index d7280c5d..13287182 100644 --- a/context.go +++ b/context.go @@ -290,6 +290,18 @@ func (c *Context) Get(key any) (value any, exists bool) { return } +// GetAs returns the value for the given key, ie: (value, true). +// If the value does not exist it returns (nil, false) +func (c *Context) GetAs[T any](key string) (result T, exists bool) { + value, exists := c.Get(key) + if !exists { + return + } + + result, exists = value.(T) + return +} + // MustGet returns the value for the given key if it exists, otherwise it panics. func (c *Context) MustGet(key any) any { if value, exists := c.Get(key); exists {