In Go language, atomic packages supply lower level atomic memory that is helpful is implementing synchronization algorithms. The LoadUint64() function in Go language is used to atomically load *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 LoadUint64(addr *uint64) (val uint64)
Here, addr indicates address.
Note: (*uint64) is the pointer to a uint64 value. However, uint64 contains the set of all unsigned 64-bit integers from 0 to 18446744073709551615.
Return value: It returns the value loaded to the *addr.
Example 1:
// Program to illustrate the usage of // LoadUint64 function in Golang // Including main package package main // importing fmt and sync/atomic import ( "fmt"
"sync/atomic"
) // Main function func main() { // Assigning values to the uint64
var (
i uint64 = 587786787
j uint64 = 9
k uint64 = 78678844556666
l uint64 = 3446
)
// Calling LoadUint64 method
// with its parameters
load_1 := atomic.LoadUint64(&i)
load_2 := atomic.LoadUint64(&j)
load_3 := atomic.LoadUint64(&k)
load_4 := atomic.LoadUint64(&l)
// Displays the uint64 value
// loaded in the *addr
fmt.Println(load_1)
fmt.Println(load_2)
fmt.Println(load_3)
fmt.Println(load_4)
} |
Output:
587786787 9 78678844556666 3446
Example 2:
// Program to illustrate the usage of // LoadUint64 function in Golang // Including main package package main // Importing fmt and sync/atomic import ( "fmt"
"sync/atomic"
) // Main function func main() { // Declaring u
var u uint64
// For loop
for i := 4; i < 200; i += 1 {
// Function with AddUint64 method
go func() {
atomic.AddUint64(&u, 6)
}()
}
// Prints loaded values address
fmt.Println(atomic.LoadUint64(&u))
} |
Output:
1068 // A random value is returned in each run
In the above example, the new values are returned from AddUint64() method in each call until the loop stops, LoadUint64() method loads these new uint64 values. And these values are stored in different addresses which can be random one so, the output of the LoadUint32() method here in each run is different. So, here a random value is returned in the output.