Skip to content

Commit

Permalink
update context.go Get/Set method use defer (#3429)
Browse files Browse the repository at this point in the history
Using defer to unlock  is more in line with go standards
  • Loading branch information
youngxhui committed Jan 2, 2023
1 parent 41f2669 commit 7d8fc15
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions context.go
Expand Up @@ -248,20 +248,20 @@ func (c *Context) Error(err error) *Error {
// It also lazy initializes c.Keys if it was not used previously.
func (c *Context) Set(key string, value any) {
c.mu.Lock()
defer c.mu.Unlock()
if c.Keys == nil {
c.Keys = make(map[string]any)
}

c.Keys[key] = value
c.mu.Unlock()
}

// Get returns the value for the given key, ie: (value, true).
// If the value does not exist it returns (nil, false)
func (c *Context) Get(key string) (value any, exists bool) {
c.mu.RLock()
defer c.mu.RUnlock()
value, exists = c.Keys[key]
c.mu.RUnlock()
return
}

Expand Down

0 comments on commit 7d8fc15

Please sign in to comment.