-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
50 lines (40 loc) · 1.01 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package state
import "sync"
// Base is the base of all events.
type Base struct {
vars map[string]interface{}
varsMut sync.RWMutex
}
// NewBase creates a new Base.
func NewBase() *Base {
return &Base{
vars: make(map[string]interface{}),
}
}
func (b *Base) copy() *Base {
cp := make(map[string]interface{}, len(b.vars))
for k, v := range b.vars {
cp[k] = v
}
return &Base{vars: cp}
}
// Set stores the passed element under the given key.
func (b *Base) Set(key string, val interface{}) {
b.varsMut.Lock()
b.vars[key] = val
b.varsMut.Unlock()
}
// Get gets the element with the passed key.
func (b *Base) Get(key string) (val interface{}) {
val, _ = b.Lookup(key)
return val
}
// Lookup returns the element with the passed key.
// Additionally, it specifies with the second return parameter, if the element
// exists, acting similar to a two parameter map lookup.
func (b *Base) Lookup(key string) (val interface{}, ok bool) {
b.varsMut.RLock()
defer b.varsMut.RUnlock()
val, ok = b.vars[key]
return
}