mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-10 06:28:24 +08:00
Merge 0d9e3d19aff81c1fc40fbaea901c99b08ff48ad7 into f5c267d2f84ba8192dd9c4c67b0490abfa3dfe7a
This commit is contained in:
commit
39caf0825a
12
context.go
12
context.go
@ -292,6 +292,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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user