In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The SwapInt32() function in Go language is used to atomically store new value into *addr and returns the previous *addr value. This function is defined under the atomic package. Here, you need to import “sync/atomic” package in order to use these functions.
func SwapInt32(addr *int32, new int32) (old int32)
Here, addr indicates address. And new is the new int32 value and old is the older int32 value.
Note: (*int32) is the pointer to a int32 value. However, int32 contains the set of all signed 32-bit integers from -2147483648 to 2147483647.
Return value: It stores the new int32 value into the *addr and returns the previous *addr value.
Stored new value: 6365654, Old value: 5435435
Here, the oldVal1 is equal to “m” as the new value to be stored in the *addr is same as the old value so true is returned but oldVal2 is not equal to “n” as there the old value is not similar to the newly assigned value hence, false is returned.