Skip to content

Instantly share code, notes, and snippets.

@debnath
Last active June 4, 2024 18:42
Show Gist options
  • Save debnath/e11de2e10ec36055eda9e446b536874e to your computer and use it in GitHub Desktop.
Save debnath/e11de2e10ec36055eda9e446b536874e to your computer and use it in GitHub Desktop.
Example usage of sync.Map: Store(), Load() and Range()
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