Avoid empty critical sections

Metadata

ID: go-best-practices/avoid-empty-critical-sections

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()
    mutex.Unlock()
}

Compliant Code Examples

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