Open In App

atomic.Load() Function in Golang With Examples

Improve
Improve
Like Article
Like
Save
Share
Report

In Go language, atomic packages supply lower-level atomic memory that is helpful is implementing synchronization algorithms. The Load() function in Go language is used to check the value set of the most current values as stored by Store method. Moreover, it can also return nil if no calls to Store method has been done for this Value. This function is defined under the atomic package. Here, you need to import “sync/atomic” package in order to use these functions.

Syntax:

func (v *Value) Load() (x interface{})

Here, v is the value of any type and x is the interface which is the output result type of Load as well as Store method.

Note: (*Value) is the pointer to a Value type. And Value type supplied in the sync/atomic standard package is used to atomically load as well as store values of any type.

Return Value: It returns the value set stored by the store method. And can also return nil if store method is not called.

Example 1:




// Program to illustrate the usage of
// Load function in Golang
  
// Including main package
package main
  
// importing fmt and sync/atomic
import (
    "fmt"
    "sync/atomic"
)
  
// Main function
func main() {
  
    // Defining a struct type L
    type L struct{ x, y, z int }
  
    // Defining a variable to assign
    // values to the struct type L
    var r1 = L{9, 10, 11}
  
    // Defining Value type to store
    // values of any type
    var V atomic.Value
  
    // Calling Store function
    V.Store(r1)
  
    // Calling Load method
    var r2 = V.Load().(L)
  
    // Prints values as
    // stored by recent
    // store method
    fmt.Println(r2)
  
    // Displays true if satisfied
    fmt.Println(r1 == r2)
}


Output:

{9 10 11}
true

In the above example, we have used Value type in order to store the values of any type. And these values are stored at r1 which is the interface stated. However, these values can be returned using the Load method.

Example 2:




// Program to illustrate the usage of
// Load function in Golang
  
// Including main package
package main
  
// importing fmt and sync/atomic
import (
    "fmt"
    "sync/atomic"
)
  
// Main function
func main() {
  
    // Defining a struct type L
    type L struct{ x, y, z int }
  
    // Defining a variable to assign
    // values to the struct type L
    var r1 = L{9, 10, 11}
  
    // Defining Value type to store
    // values of any type
    var V atomic.Value
  
    // Calling Load method
    var r2 = V.Load().(L)
  
    // Prints values as 
    // stored by recent
    // store method
    fmt.Println(r2)
  
    // Displays true if satisfied
    fmt.Println(r1 == r2)
}


Output:

panic: interface conversion: interface {} is nil, not main.L

goroutine 1 [running]:
main.main()
    /tmp/sandbox731326366/prog.go:28 +0x240

Here, no call to store method has been done so nil is returned.



Last Updated : 01 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads