GoGuard
GoGuard is a tool to generate simple resource guards.
Usage
Generate a guard for type *Resource with name ResourceGuard at file resource_guard.go:
goguard *Resource Resourceusage in code:
g := NewIntGuard(10)
g.Open(func(v int){
//v's access is synchronized
})Advantage
The conventional way of synchroning a resource:
mu := &sync.Mutex{}
resource := Resource{}
mu.Lock()
//use it
mu.Unlock()Using a guard:
guard := NewResourceGuard(&Resource{})
guard.Open(func(r *Resource){
//use it
})- guard is more succint and, I think, more declarative.
- guard limits the scope of a resource to where it's safe to use and thus it's hard for racey mistakes to occur.
- guard prevents your code/structs from being cluttered with mutexes.