Do not defer Lock

Metadata

ID: go-best-practices/defer-lock

Language: Go

Severity: Info

Category: Best Practices

Description

Empty critical sections are often a mistake. Instead of unlocking, developers often miss using defer to defer unlocking the mutex.

Non-Compliant Code Examples

func test() {
    mutex.Lock()
    defer mutex.Lock()
}

Compliant Code Examples

func test() {
    mutex.Lock()
    defer mutex.Unlock()
}