Sitelet https://web.archive.org/web/20190322180511/https://github.com/codercom/goguard
Skip to content
Please note that GitHub no longer supports Internet Explorer.

We recommend upgrading to the latest Microsoft Edge, Google Chrome, or Firefox.

Learn more
Synchronize access to Go resources without stress
Branch: master
Clone or download
Latest commit 98b03e5 Mar 10, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
.gitignore
README.md
bindata.go
guard.tmpl
main.go

README.md

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 Resource

usage 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.
You can’t perform that action at this time.