update context.go Get/Set method use defer (#3429)

Using defer to unlock  is more in line with go standards
This commit is contained in:
youngxhui 2023-01-02 11:39:26 +08:00 committed by GitHub
parent 41f2669ebc
commit 7d8fc1563b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 2 deletions

View File

@ -248,20 +248,20 @@ func (c *Context) Error(err error) *Error {
// It also lazy initializes c.Keys if it was not used previously. // It also lazy initializes c.Keys if it was not used previously.
func (c *Context) Set(key string, value any) { func (c *Context) Set(key string, value any) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock()
if c.Keys == nil { if c.Keys == nil {
c.Keys = make(map[string]any) c.Keys = make(map[string]any)
} }
c.Keys[key] = value c.Keys[key] = value
c.mu.Unlock()
} }
// Get returns the value for the given key, ie: (value, true). // Get returns the value for the given key, ie: (value, true).
// If the value does not exist it returns (nil, false) // If the value does not exist it returns (nil, false)
func (c *Context) Get(key string) (value any, exists bool) { func (c *Context) Get(key string) (value any, exists bool) {
c.mu.RLock() c.mu.RLock()
defer c.mu.RUnlock()
value, exists = c.Keys[key] value, exists = c.Keys[key]
c.mu.RUnlock()
return return
} }