Skip to content

Instantly share code, notes, and snippets.

@igm
Created December 10, 2012 22:19
Show Gist options
  • Save igm/4253873 to your computer and use it in GitHub Desktop.
Save igm/4253873 to your computer and use it in GitHub Desktop.
Lock based map
package main
import (
"sync"
)
type conn struct{}
type connectionMap struct {
connections map[string]*conn
lock sync.Mutex
}
func NewMap() *connectionMap {
return &connectionMap{
connections: make(map[string]*conn),
}
}
func (cm *connectionMap) get(key string) *conn {
cm.lock.Lock()
defer cm.lock.Unlock()
return cm.connections[key]
}
func (cm *connectionMap) set(key string, c *conn) {
cm.lock.Lock()
defer cm.lock.Unlock()
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