Skip to content

Instantly share code, notes, and snippets.

@arpchaudhary
Last active July 12, 2018 13:10
Show Gist options
  • Save arpchaudhary/209219949c9b5c217e103e0bc4affce2 to your computer and use it in GitHub Desktop.
Save arpchaudhary/209219949c9b5c217e103e0bc4affce2 to your computer and use it in GitHub Desktop.
Concurrent Hash Map in Go
package main
import (
"fmt"
"sync"
)
var (
PROC_MAP = struct {
sync.RWMutex
m map[string]bool
}{m: make(map[string]bool)}
)
func record(data string) {
PROC_MAP.Lock()
PROC_MAP.m[data] = true
PROC_MAP.Unlock()
}
func verify(data string) bool {
PROC_MAP.RLock()
status := PROC_MAP.m[data]
PROC_MAP.RUnlock()
return status
}
func main() {
record("bar")
record("foo")
done := make(chan bool)
go func() {
fmt.Println("Verify [bar]", verify("bar"))
fmt.Println("Verify [foobar]", verify("foobar"))
fmt.Println("Verify [foo]", verify("foo"))
done <- true
}()
<-done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment