Inspectopedia Help

Incorrect usage of the 'sync/atomic' package

Reports assignment statements of the form x = atomic.AddUint64(&x, 1).

Such operations are not atomic, and is a common misuse of the sync/atomic API. To make them atomic, one need to remove the assignment to use a direct call: atomic.AddUint64(&x, 1). In that case, the value of x will be updated atomically by address.

Example:

import ( "sync/atomic" ) type Counter uint64 func AtomicTests() { x := uint64(1) x = atomic.AddUint64(&x, 1) // ERROR "direct assignment to atomic value" _, x = 10, atomic.AddUint64(&x, 1) // ERROR "direct assignment to atomic value" x, _ = atomic.AddUint64(&x, 1), 10 // ERROR "direct assignment to atomic value" }

Inspection Details

Available in:

GoLand 2023.3

Plugin:

Go, 233.SNAPSHOT

Last modified: 13 July 2023