add iterator to Keys

This commit is contained in:
nil 2021-11-13 10:55:02 +08:00 committed by GitHub
parent 6aee45608d
commit 388bc45569
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -260,6 +260,16 @@ func (c *Context) Get(key string) (value interface{}, exists bool) {
return
}
// Do iterates the key/value pairs stored in c.Keys and executes the callback.
// It does nothing if c.Keys is nil or empty.
func (c *Context) Do(fn func(key string, value interface{})) {
c.mu.RLock()
for k, v := range c.Keys {
fn(k,v)
}
c.mu.RUnlock()
}
// MustGet returns the value for the given key if it exists, otherwise it panics.
func (c *Context) MustGet(key string) interface{} {
if value, exists := c.Get(key); exists {