In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The StoreUint64() function in Go language is used to atomically store val into *addr. This function is defined under the atomic package. Here, you need to import “sync/atomic” package in order to use these functions.
Syntax:
func StoreUint64(addr *uint64, val uint64)
Here, addr indicates address.
Note: (*uint64) is the pointer to a uint64 value. And uint64 is an integer type of bit size 64. However, int64 contains the set of all unsigned 64-bit integers from 0 to 18446744073709551615.
Return Value: It stores the val into *addr and then can be returned when required.
Example 1:
// Program to illustrate the usage of // StoreUint64 function in Golang // Including main package package main // importing fmt and sync/atomic import ( "fmt"
"sync/atomic"
) // Main function func main() { // Defining variables for the
// address to store the val
var (
x uint64
y uint64
)
// Using StoreUint64 method
// with its parameters
atomic.StoreUint64(&x, 56576656555555)
atomic.StoreUint64(&y, 0)
// Displays the value
// stored in addr
fmt.Println(atomic.LoadUint64(&x))
fmt.Println(atomic.LoadUint64(&y))
} |
Output:
56576656555555 0
Here, first, the uint64 value is stored in the addresses defined then they are returned using the LoadUint64() method above.
Example 2:
// Program to illustrate the usage of // StoreUint64 function in Golang // Including main package package main // importing fmt and sync/atomic import ( "fmt"
"sync/atomic"
) // Main function func main() { // Defining variables for
// the address to store the val
var (
x uint64
)
// Using StoreUint64 method
// with its parameters
atomic.StoreUint64(&x, 111776540544)
// Loading the stored val
z := atomic.LoadUint64(&x)
// Prints true if values
// are same else false
fmt.Println(z == x)
// Prints true if addresses
// are same else false
fmt.Println(&z == &x)
} |
Output:
true false
Here, the value stored and loaded are the same so true is returned but their addresses are not the same so false is returned in that case.