Atomicity makes an operation indivisible: competing goroutines cannot interleave its accesses and lose an update. A plain i++ can read, increment, and write separately, producing 1 instead of 2. section 8.4.1
var i int64
go func() {
atomic.AddInt64(&i, 1) #1
}()
go func() {
atomic.AddInt64(&i, 1) #2
}()
Use sync/atomic to increment an int64 as one operation, regardless of goroutine execution order. section 8.4.1
Choose the synchronization boundary that matches the data:
- Use sync/atomic for supported numeric types such as int64. section 8.4.1
- Use a mutex when the critical section involves slices, maps, structs, or other unsupported data. section 8.4.1
- Use a channel when goroutines can communicate increments instead of sharing the same memory location. section 8.4.1












