wp-go/safety/slice.go
2024-06-12 00:14:07 +08:00

40 lines
530 B
Go

package safety
import "sync"
type Slice[T any] struct {
Var []T
mu sync.RWMutex
}
func NewSlice[T any](a ...[]T) *Slice[T] {
var s []T
if len(a) > 0 {
s = a[0]
}
return &Slice[T]{
s,
sync.RWMutex{},
}
}
func (r *Slice[T]) Append(t ...T) {
r.mu.Lock()
defer r.mu.Unlock()
r.Var = append(r.Var, t...)
}
func (r *Slice[T]) Store(a []T) {
r.mu.Lock()
defer r.mu.Unlock()
r.Var = a
}
func (r *Slice[T]) Load() (a []T) {
r.mu.RLock()
defer r.mu.RUnlock()
a = make([]T, len(r.Var))
copy(a, r.Var)
return
}