Menu Close

Go – LoadInt64() function in sync/atomic package

In this article, we will explore the LoadInt64() Function in sync/atomic package in Go in details, along with examples.

The LoadInt64 function is part of the sync/atomic package in Go. It performs an atomic read operation on an int64 value. The function prototype is as follows:


func LoadInt64(addr *int64) (val int64)


  • addr: A pointer to the memory address storing the int64 value that needs to be read atomically.

Return Value:

  • val: The int64 value read from the memory address.

The LoadInt64 function performs an atomic operation, ensuring that the read occurs without being interrupted by other goroutines. This is crucial when working with shared memory in concurrent code.


Let’s consider a simple example to demonstrate the use of the LoadInt64 function. Suppose we have a shared int64 value that multiple goroutines read concurrently.

package main

import (






func main() {

	var sharedValue int64 = 100

	var wg sync.WaitGroup

	read := func(id int) {

		defer wg.Done()

		time.Sleep(time.Duration(id) * 10 * time.Millisecond)

		val := atomic.LoadInt64(&sharedValue)

		fmt.Printf("Goroutine %d - Read value: %d\n", id, val)



	for i := 1; i <= 5; i++ {

		go read(i)




In this example, we have an int64 sharedValue variable that we want to read atomically. We use the LoadInt64 function to ensure that the sharedValue is read safely by multiple goroutines. Each goroutine sleeps for a specific duration before reading the value, simulating a real-world scenario where goroutines perform work before accessing shared memory.


The LoadInt64 function in Go’s sync/atomic package provides a low-level atomic operation for safe access to shared memory in concurrent code. When working with shared memory in concurrent programming, it is essential to use atomic operations to prevent data races and ensure data consistency. This article has provided a detailed explanation of the LoadInt64 function and demonstrated its usage through an example.

To check more Go related articles. Pls click given below link:[email protected]

Posted in golang, packages, sync

Leave a Reply

Your email address will not be published. Required fields are marked *