Documentation
¶
Overview ¶
Counter is a library to simplify the use of sync/atomic.Uint64 values as counters in applications.
Example ¶
package main import ( "fmt" "log" "catinello.eu/counter" ) func main() { c := counter.NewCounters() done := make(chan bool) go func(c *counter.Counters, quit chan<- bool) { for i := 0; i < 5000; i++ { c.Increment("test") } quit <- true }(c, done) <-done v, err := c.Get("test") if err != nil { log.Fatal(err) } fmt.Println(v) }
Output: 5000
Index ¶
- Variables
- type Counter
- type Counters
- func (c *Counters) Add(name any, value uint64) error
- func (c *Counters) Decrement(name any) error
- func (c *Counters) Delete(name any) error
- func (c *Counters) Get(name any) (uint64, error)
- func (c *Counters) Increment(name any) error
- func (c *Counters) Is(name any, value uint64) bool
- func (c *Counters) List() []entity
- func (c *Counters) Reset(name any) uint64
- func (c *Counters) Set(name any, value uint64) uint64
- func (c *Counters) Store(path string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Create a new counter on methods Add/Increment/Decrement if not already present in *Counters. // Set() can still create unknown counters, even if AutoCreate is set to false. AutoCreate bool = true )
Functions ¶
This section is empty.
Types ¶
type Counter ¶
Counter is a single atomic uint64.
type Counters ¶
type Counters struct {
// contains filtered or unexported fields
}
Counters contains multiple named atomic uint64 that are mutex protected.
func Load ¶
Load is a convenience function to read Counters from a file at the given path.
See (c *Counters) Store(path string) for saving data.
func (*Counters) Add ¶
Add takes a value for the given name and adds it to it's value or creates a new Counter with that value.
func (*Counters) Get ¶
Get returns the uint64 value for given name or an error if it does not exist.
func (*Counters) Is ¶
Is compares the Counter with the value and the given name and returns a boolean.
func (*Counters) List ¶
func (c *Counters) List() []entity
List returns all available Counters with Name and Value.