package counter import "sync" // Counter is a goroutine-safe integer counter. type Counter struct { mu sync.Mutex value int64 } // New returns a counter initialized to initial. func New(initial int64) *Counter { return &Counter{value: initial} } // Inc increments the counter by one and returns the new value. func (c *Counter) Inc() int64 { return c.Add(1) } // Add adds delta to the counter 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 } // Value returns the current counter value. func (c *Counter) Value() int64 { c.mu.Lock() defer c.mu.Unlock() return c.value }