Guest User

Untitled

a guest
Apr 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. // Provides Write once read many (WORM) primitives
  2. package worm
  3.  
  4. import (
  5. "sync"
  6. )
  7.  
  8. // Object could be used to store interface{}
  9. // Object is safe for concurrent usage
  10. type Object struct {
  11. sync.RWMutex
  12. sealed bool
  13. v interface{}
  14. }
  15.  
  16. func NewWithValue(v interface{}) Object {
  17. return Object{
  18. sealed: true,
  19. v: v,
  20. }
  21. }
  22.  
  23. func (o Object) Set(v interface{}) {
  24. o.Lock()
  25. defer o.Unlock()
  26.  
  27. if o.sealed {
  28. return
  29. }
  30.  
  31. o.v = v
  32. o.sealed = true
  33. }
  34.  
  35. func (o Object) Value() interface{} {
  36. o.RLock()
  37. v := o.v
  38. o.RUnlock()
  39. return v
  40. }
Add Comment
Please, Sign In to add comment