In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The StoreInt64() 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 StoreInt64(addr *int64, val int64)
Here, addr indicates address.
Note: (*int64) is the pointer to a int64 value. However, int64 contains the set of all signed 64-bit integers from -9223372036854775808 to 9223372036854775807.
Return value: It stores the val into *addr and then can be returned when required.
Example 1:
package main
import (
"fmt"
"sync/atomic"
)
func main() {
var (
x int64
y int64
)
atomic.StoreInt64(&x, 6777676777)
atomic.StoreInt64(&y, 98877)
fmt.Println(atomic.LoadInt64(&x))
fmt.Println(atomic.LoadInt64(&y))
}
|
Output:
6777676777
98877
Here, first, the int64 value is stored in the addresses defined then they are returned using the LoadInt64() method above.
Example 2:
package main
import (
"fmt"
"sync/atomic"
)
func main() {
var (
x int64
)
atomic.StoreInt64(&x, 3654567899788)
z := atomic.LoadInt64(&x)
fmt.Println(z == x)
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!