Compare commits

...

3 Commits

Author SHA1 Message Date
Cheikh Seck
26174d1283
Merge 0d9e3d19aff81c1fc40fbaea901c99b08ff48ad7 into 915e4c90d28ec4cffc6eb146e208ab5a65eac772 2025-12-27 21:26:16 +08:00
Cheikh Seck
0d9e3d19af
Update context.go
use method Get to get existing data.
2022-09-19 12:06:54 +00:00
Cheikh Seck
84bae816a0
Update context to have a generic method.
Add a generic method `GetAs` to get request data.
2022-09-19 12:00:08 +00:00

View File

@ -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 {