Standard library / sync/atomic
atomic.AddInt32()
Atomically add delta and return the new value. Integer overflow follows the type width. Raw pointer APIs require correctly aligned storage; prefer the corresponding typed atomic in new code. Expected stdout: '15\n15\n'.
Formal Syntax & Structure
go syntaxgo
func AddInt32(addr *int32, delta int32) (new int32)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| addr | *int32 | Pointer to the shared variable; satisfy architecture alignment requirements and use atomic access consistently. |
| delta | int32 | Amount to add; for WaitGroup, change in outstanding task count. |
Return Value
(new int32)
New value after addition.
Working Example
Example Snippetgo
package main
import ( "fmt" "sync/atomic")
func main() { x := new(int32) atomic.StoreInt32(x, 12) fmt.Println(atomic.AddInt32(x, 3)) fmt.Println(atomic.LoadInt32(x))}