In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The StoreInt32() 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 StoreInt32(addr *int32, val int32)
Here, addr indicates address.
Note: (*int32) is the pointer to a int32 value. However, int32 contains the set of all signed 32-bit integers from -2147483648 to 2147483647.
Return value: It stores the val into *addr and then can be returned when required.
Example 1:
// Program to illustrate the usage of // StoreInt32 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 int32 y int32 ) // Using StoreInt32 method // with its parameters atomic.StoreInt32(&x, 65) atomic.StoreInt32(&y, 3455) // Displays the value stored in addr fmt.Println(atomic.LoadInt32(&x)) fmt.Println(atomic.LoadInt32(&y)) } |
Output:
65 3455
Here, first, the int32 value is stored in the addresses defined then they are returned using the LoadInt32() method above.
Example 2:
// Program to illustrate the usage of // StoreInt32 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 int32 ) // Using StoreInt32 method // with its parameters atomic.StoreInt32(&x, 8943) // Loading the stored val z := atomic.LoadInt32(&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.