try sync.Map in golang 1.9

2017-09-01
golang

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

package main

import (
"fmt"
"sync"
"time"
)

func writeProc(m *sync.Map, c chan int) {

go func() {

i := 0
for {
v, ok := m.Load("ok1")

if ok {
fmt.Println("get the ok 1")
fmt.Println(v)
c <- 1
return
}

fmt.Printf("does not get the key element, sleep for one second, counting: %d\n", i)
i++
time.Sleep(1000 * time.Millisecond)
}
}()
}

func main() {

m := new(sync.Map)

c := make(chan int)

writeProc(m, c)

time.Sleep(10 * time.Second)
m.Store("ok1", "goood!!!")
<-c
}

output example:

1
2
3
4
5
6
7
8
9
10
11
12
13
go 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!!!