atomic.Load() Function in Golang With Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
}

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.