Inspectopedia
 
2024.3

Incorrect usage of the 'sync/atomic' package

Warning
Reliability
New
Last modified: 03 December 2024

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: