Home » Go Atomic Variable

Go Atomic Variable

Atomic variables are used to manage state, though sync/atomic package and avoid race conditions. Atomic counters can be accessed by multiple go routines.

Go Atomic Variable Example

Output:

foo:  0 Count -> 1 foo:  1 Count -> 2 bar:  0 Count -> 3 bar:  1 Count -> 4 bar:  2 Count -> 5 foo:  2 Count -> 6 bar:  3 Count -> 7 bar:  4 Count -> 8 bar:  5 Count -> 9 foo:  3 Count -> 10 bar:  6 Count -> 11 bar:  7 Count -> 12 foo:  4 Count -> 13 foo:  5 Count -> 14 bar:  8 Count -> 15 bar:  9 Count -> 16 foo:  6 Count -> 17 foo:  7 Count -> 18 foo:  8 Count -> 19 foo:  9 Count -> 20 last count value  20 

Next TopicGo Channel

You may also like