Skip to content

Instantly share code, notes, and snippets.

@deckarep
Last active September 10, 2017 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deckarep/ccd0b55743f323ee6ec4f35feae52aaf to your computer and use it in GitHub Desktop.
Save deckarep/ccd0b55743f323ee6ec4f35feae52aaf to your computer and use it in GitHub Desktop.
Usage of sync.Map
func syncMapUsage() {
fmt.Println("sync.Map test (Go 1.9+ only)")
fmt.Println("----------------------------")
// Create the threadsafe map.
var sm sync.Map
// Fetch an item that doesn't exist yet.
result, ok := sm.Load("hello")
if ok {
fmt.Println(result.(string))
} else {
fmt.Println("value not found for key: `hello`")
}
// Store an item in the map.
sm.Store("hello", "world")
fmt.Println("added value: `world` for key: `hello`")
// Fetch the item we just stored.
result, ok = sm.Load("hello")
if ok {
fmt.Printf("result: `%s` found for key: `hello`\n", result.(string))
}
fmt.Println("---------------------------")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment