In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The CompareAndSwapUint64() function in Go language is used to perform the compare and swap operation for an uint64 value. This function is defined under the atomic package. Here, you need to import “sync/atomic” package in order to use these functions.
func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool)
Here, addr indicates address, old indicates uint64 value that is old one, and new is the uint64 new value that will swap itself from the old value.
Note: (*uint64) is the pointer to a uint64 value. And uint64 is integer type of bit size 64. Moreover, int64 contains the set of all unsigned 64-bit integers ranging from 0 to 18446744073709551615.
Return Value: It returns true if swapping is accomplished else it returns false.
true The new value of i is: 575765878
Swapped_value: 4676778904 , old_value: 143255757 false The value of i is: 4676778904
Here, the swapped value obtained from the swapping operation must be the old value. i.e. 4676778904 that’s why false is returned.