wp-go/safety/slice.go

24 lines
297 B
Go
Raw Permalink Normal View History

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()
2023-11-12 13:39:04 +00:00
defer r.mu.Unlock()
ts := append(r.Load(), t...)
r.Store(ts)
2023-11-12 13:39:04 +00:00
}