package dbUtils import "sync/atomic" type ICounter[i int32 | int64] interface { Set(new i) (old i) Add(delta i) (val i) Get() (val i) } // 32 bit int counter implementation of ICounter type Counter32 struct { val int32 } // 32 bit int counter implementation of ICounter func NewCounter32(initVal int32) ICounter[int32] { return &Counter32{val: initVal} } func (c *Counter32) Set(new int32) (old int32) { return atomic.SwapInt32(&c.val, new) } func (c *Counter32) Add(delta int32) (val int32) { return atomic.AddInt32(&c.val, delta) } func (c *Counter32) Get() (val int32) { return atomic.LoadInt32(&c.val) } // 64 bit int counter implementation of ICounter type Counter64 struct { val int64 } func NewCounter64(initVal int64) ICounter[int64] { return &Counter64{val: initVal} } func (c *Counter64) Set(new int64) (old int64) { return atomic.SwapInt64(&c.val, new) } func (c *Counter64) Add(delta int64) (val int64) { return atomic.AddInt64(&c.val, delta) } func (c *Counter64) Get() (val int64) { return atomic.LoadInt64(&c.val) }