Created
April 27, 2017 13:23
-
-
Save aryszka/6378c0427434a56a4f881bfe51099c2f to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import "fmt" | |
type syncMap chan map[string]int | |
func newSyncMap() syncMap { | |
m := make(map[string]int) | |
sm := make(syncMap, 1) | |
sm <- m | |
return sm | |
} | |
func (sm syncMap) get(key string) (int, bool) { | |
m := <-sm | |
v, ok := m[key] | |
sm <- m | |
return v, ok | |
} | |
func (sm syncMap) set(key string, val int) { | |
m := <-sm | |
m[key] = val | |
sm <- m | |
} | |
func main() { | |
m := newSyncMap() | |
m.set("foo", 1) | |
m.set("bar", 2) | |
foo, ok := m.get("foo") | |
fmt.Println(foo, ok) | |
bar, ok := m.get("bar") | |
fmt.Println(bar, ok) | |
baz, ok := m.get("baz") | |
fmt.Println(baz, ok) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment