Created
April 13, 2018 13:51
-
-
Save dmotylev/bf7030d0a9dc055239b7de93bc071864 to your computer and use it in GitHub Desktop.
Write once read many object
This file contains 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
// 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