Golang program to demonstrate the atomic.LoadInt32() function

Here, we are going to demonstrate the atomic.LoadInt32() function in Golang (Go Language).
Submitted by Nidhi, on May 04, 2021 [Last updated : March 05, 2023]

atomic.LoadInt32() function in Golang

Problem Solution:

Here, we will loaded the value of 32-bit integer variable using atomic.LoadInt32() function. In the atomic.LoadInt32() function, we passed address of variables. The atomic.LoadInt32() function is used the values of variables in multithreaded/multi-process environment.

Program/Source Code:

The source code to demonstrate the atomic.LoadInt32() function is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

Golang code to demonstrate the example of atomic.LoadInt32() function

// Golang program to demonstrate the
// atomic.LoadInt32() function

package main

import "fmt"
import "sync/atomic"

//Entry point for the program
func main() {
	var num1 int32 = 786
	var num2 int32 = 111

	loadVal1 := atomic.LoadInt32(&num1)
	loadVal2 := atomic.LoadInt32(&num2)

	//Print 32-bit integer loaded value
	fmt.Println(loadVal1)
	fmt.Println(loadVal2)
}

Output:

786
111

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the required packages to predefined functions.

In the main() function, we created two 32-bit integer variables num1, num2 that are initialized with 786, 111 respectively. Then we loaded the value of 32-bit integers using the atomic.LoadInt32() function. The atomic.LoadInt32() function is used the values of variables in multithreaded/multi-process environment. At last, we printed the loaded value of variables on the console screen.

Golang sync/atomic Package Programs »





All Golang Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.