Skip to content

Instantly share code, notes, and snippets.

@igm
Created December 10, 2012 22:33
Show Gist options
  • Save igm/4253953 to your computer and use it in GitHub Desktop.
Save igm/4253953 to your computer and use it in GitHub Desktop.
Channel Based Map
package main
type conn struct{}
type connectionMap struct {
connections map[string]*conn
actions chan func()
}
func NewMap() *connectionMap {
res := &connectionMap{
connections: make(map[string]*conn),
actions: make(chan func()),
}
go func() {
for action := range res.actions {
action()
}
}()
return res
}
func (cm *connectionMap) get(key string) (c *conn) {
ret := make(chan bool)
cm.actions <- func() {
c = cm.connections[key]
ret <- true
}
<-ret
return
}
func (cm *connectionMap) set(key string, c *conn) {
cm.actions <- func() { cm.connections[key] = c }
}
func main() {
myMap := NewMap()
myMap.set("key", new(conn))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment