In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The CompareAndSwapUintptr() function in Go language is used to perform the compare and swap operation for an uintptr value. This function is defined under the atomic package. Here, you need to import “sync/atomic” package in order to use these functions.
func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool)
Here, addr indicates address, old indicates uintptr value that is old one, and new is the uintptr new value that will swap itself from the old value is stored at addr.
Note: (*uintptr) is the pointer to a uintptr value. And uintptr is an unsigned integer type that is too large and contains a bit pattern of any pointer.
Return Value: It returns true if swapping is accomplished else it returns false.
true true true 647567565 76 15
Swapped_value: 2344444, old_value: 56466244 false 2344444
Here, the swapped value obtained from the swapping operation must be the old value of the CompareAndSwapUintptr() method but here the old value taken is the old value of swapping operation which is not correct that’s why false is returned.