In Go language, atomic packages supply lower level atomic memory that is helpful is implementing synchronization algorithms. The AddUint64() function in Go language is used to automically add delta to the *addr. This function is defined under the atomic package. Here, you need to import “sync/atomic” package in order to use these functions.
func AddUint64(addr *uint64, delta uint64) (new uint64)
Here, addr indicates address and delta indicates a small number of bits greater than zero. Moreover, if you want to subtract a signed positive constant value c from x, then you can do it by AddUint64(&x, ^uint64(c-1)). And if you want to decrement x particularly, then it can be done by AddUint64(&x, ^uint64(0)).
Note: (*uint64) is the pointer to a uint64 value. Moreover, uint64 contains the set of all unsigned 64-bit integers from 0 to 18446744073709551615.
Return Value: It adds addr and delta automically and returns a new value.
438 155 5 18446744073709551601
325 650 975
In the above example, we have defined a function adds that returns the output returned from calling AddUint64 method. In the main function, we have defined a “for” loop that will increment the value of ‘u’ in each call. Here, the second parameter of the AddUint64() method is constant and only the value of the first parameter is variable. However, the output of the previous call will be the value of the first parameter of the AddUint64() method in the next call until the loop stops.
Lets see how above example works:
1st parameter = 0, 2nd parameter = 325 // returns (0 + 325 = 325) // Now, the above output is 1st parameter in next call to AddUint64() method 1st parameter = 325, 2nd parameter = 325 // returns (325 + 325 = 650) 1st parameter = 650, 2nd parameter = 325 // returns (650 + 325 = 975)