Menu Close

Go – LoadUint64() in sync/atomic package

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

The LoadUint64 function in go is part of the “sync/atomic” package in Go which offers low-level atomic memory operations. This function atomically loads and returns the current value of a uint64 variable. It is a read operation that ensures the value read is the most recent one written by another concurrent thread. The function signature is as follows:


func LoadUint64(addr *uint64) uint64

The function takes a pointer to a uint64 variable addr as an argument and returns the current value of the variable.


To illustrate the usage of the LoadUint64 function, let’s create a simple program simulating concurrent data access. We will create two goroutines: one that increments a shared counter and another that reads and prints the counter value using the LoadUint64 function.

package main

import (






func main() {

	var counter uint64 = 0

	var wg sync.WaitGroup

	// Incrementer goroutine

	go func() {

		defer wg.Done()

		for i := 0; i < 1000; i++ {

			atomic.AddUint64(&counter, 1)




	// Reader goroutine

	go func() {

		defer wg.Done()

		for i := 0; i < 10; i++ {

			fmt.Printf("Current counter value: %d\n", atomic.LoadUint64(&counter))

			time.Sleep(time.Millisecond * 100)





In this example, we use the sync.WaitGroup to ensure that both goroutines complete before the program exits. The incrementer goroutine increments the counter using the atomic.AddUint64 function, which performs an atomic addition. Meanwhile, the reader goroutine reads the counter value using the atomic.LoadUint64 function, guaranteeing that it always reads the latest value written by the incrementer goroutine.

This example demonstrates how the LoadUint64 function can be used to perform atomic read operations on a shared uint64 variable, ensuring data consistency across concurrent threads.


The LoadUint64 function is a valuable utility provided by the sync/atomic package in Go, enabling atomic read operations on uint64 variables. It is particularly useful in concurrent programming when multiple goroutines access shared memory. Using the LoadUint64 function guarantees data consistency and helps to eliminate race conditions, contributing to the overall stability and reliability of your concurrent Go applications.

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 *