package counter import "sync" // Counter is a thread-safe integer counter. type Counter struct { mu sync.RWMutex value int64 } // New creates a counter with the given initial value. func New(initial int64) *Counter { return &Counter{value: initial} } // Add changes the counter by delta and returns the new value. func (c *Counter) Add(delta int64) int64 { c.mu.Lock() defer c.mu.Unlock() c.value += delta return c.value } // Increment increases the counter by one and returns the new value. func (c *Counter) Increment() int64 { return c.Add(1) } // Value returns the current value. func (c *Counter) Value() int64 { c.mu.RLock() defer c.mu.RUnlock() return c.value } // Reset sets the counter to zero and returns its previous value. func (c *Counter) Reset() int64 { c.mu.Lock() defer c.mu.Unlock() previous := c.value c.value = 0 return previous }