Created
December 10, 2012 22:33
-
-
Save igm/4253953 to your computer and use it in GitHub Desktop.
Channel Based Map
This file contains hidden or 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 | |
| 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