/
bytes.go
50 lines (42 loc) · 988 Bytes
/
bytes.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
// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
package gtype
import (
"sync"
)
type Bytes struct {
mu sync.RWMutex
val []byte
}
func NewBytes(value...[]byte) *Bytes {
if len(value) > 0 {
return &Bytes{val:value[0]}
}
return &Bytes{}
}
func (t *Bytes)Set(value []byte) {
t.mu.Lock()
t.val = value
t.mu.Unlock()
}
func (t *Bytes)Val() []byte {
t.mu.RLock()
b := t.val
t.mu.RUnlock()
return b
}
// 使用自定义方法执行加锁修改操作
func (t *Bytes) LockFunc(f func(value []byte) []byte) {
t.mu.Lock()
t.val = f(t.val)
t.mu.Unlock()
}
// 使用自定义方法执行加锁读取操作
func (t *Bytes) RLockFunc(f func(value []byte)) {
t.mu.RLock()
f(t.val)
t.mu.RUnlock()
}