Skip to content

Instantly share code, notes, and snippets.

@dmotylev
Created April 13, 2018 13:51
Show Gist options
  • Save dmotylev/bf7030d0a9dc055239b7de93bc071864 to your computer and use it in GitHub Desktop.
Save dmotylev/bf7030d0a9dc055239b7de93bc071864 to your computer and use it in GitHub Desktop.
Write once read many object
// Provides Write once read many (WORM) primitives
package worm
import (
"sync"
)
// Object could be used to store interface{}
// Object is safe for concurrent usage
type Object struct {
sync.RWMutex
sealed bool
v interface{}
}
func NewWithValue(v interface{}) Object {
return Object{
sealed: true,
v: v,
}
}
func (o Object) Set(v interface{}) {
o.Lock()
defer o.Unlock()
if o.sealed {
return
}
o.v = v
o.sealed = true
}
func (o Object) Value() interface{} {
o.RLock()
v := o.v
o.RUnlock()
return v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment