Last active
September 10, 2017 01:11
-
-
Save deckarep/ccd0b55743f323ee6ec4f35feae52aaf to your computer and use it in GitHub Desktop.
Usage of sync.Map
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
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