github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/elvish/store/shared_var.go (about) 1 package store 2 3 import ( 4 "errors" 5 "sync" 6 ) 7 8 type shared struct { 9 sync.Mutex 10 vars map[string]string 11 } 12 13 // ErrNoVar is returned by (*Store).GetSharedVar when there is no such variable. 14 var ErrNoVar = errors.New("no such variable") 15 16 // SharedVar gets the value of a shared variable. 17 func (vars *shared) SharedVar(n string) (string, error) { 18 vars.Lock() 19 defer vars.Unlock() 20 v, ok := vars.vars[n] 21 if !ok { 22 return "", ErrNoVar 23 } 24 return v, nil 25 } 26 27 // SetSharedVar sets the value of a shared variable. 28 func (vars *shared) SetSharedVar(n, v string) error { 29 vars.Lock() 30 defer vars.Unlock() 31 vars.vars[n] = v 32 return nil 33 } 34 35 // DelSharedVar deletes a shared variable. 36 func (vars *shared) DelSharedVar(n string) error { 37 vars.Lock() 38 defer vars.Unlock() 39 delete(vars.vars, n) 40 return nil 41 } 42 43 func NewSharedVar() *shared { 44 return &shared{vars: make(map[string]string)} 45 }