Golang 1.9 new version support safe Map now, details can be found,
https://golang.org/pkg/sync/#Map.Load
Here is sample code, test filename is ‘sync_map.go’
1 |
|
output example:1
2
3
4
5
6
7
8
9
10
11
12
13go run sync_map.go
does not get the key element, sleep for one second, counting: 0
does not get the key element, sleep for one second, counting: 1
does not get the key element, sleep for one second, counting: 2
does not get the key element, sleep for one second, counting: 3
does not get the key element, sleep for one second, counting: 4
does not get the key element, sleep for one second, counting: 5
does not get the key element, sleep for one second, counting: 6
does not get the key element, sleep for one second, counting: 7
does not get the key element, sleep for one second, counting: 8
does not get the key element, sleep for one second, counting: 9
get the ok 1
goood!!!