wp-go/safety/slice.go
2023-11-12 21:39:04 +08:00

24 lines
297 B
Go

package safety
import "sync"
type Slice[T any] struct {
*Var[[]T]
mu sync.Mutex
}
func NewSlice[T any](a []T) *Slice[T] {
return &Slice[T]{
NewVar(a),
sync.Mutex{},
}
}
func (r *Slice[T]) Append(t ...T) {
r.mu.Lock()
defer r.mu.Unlock()
ts := append(r.Load(), t...)
r.Store(ts)
}