mirror of
https://github.com/gin-gonic/gin.git
synced 2026-01-11 17:17:04 +08:00
Compare commits
3 Commits
e4dacc3b61
...
26174d1283
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
26174d1283 | ||
|
|
0d9e3d19af | ||
|
|
84bae816a0 |
12
context.go
12
context.go
@ -291,6 +291,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