Last active
June 4, 2024 18:42
-
-
Save debnath/e11de2e10ec36055eda9e446b536874e to your computer and use it in GitHub Desktop.
Example usage of sync.Map: Store(), Load() and Range()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
func main() { | |
var wg sync.WaitGroup | |
var m sync.Map | |
wg.Add(5) | |
for i := 0; i < 5; i++ { | |
go func(j int) { | |
m.Store(j, fmt.Sprintf("test %v", j)) | |
wg.Done() | |
}(i) | |
} | |
wg.Wait() | |
fmt.Println("Done.") | |
for i := 0; i < 5; i++ { | |
t, _ := m.Load(i) | |
fmt.Println("for loop: ",t) | |
} | |
m.Range(func(k, v interface{}) bool { | |
fmt.Println("range (): ", v) | |
return true | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment