counter

package module
v0.0.0-...-3f26575 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2025 License: BSD-3-Clause Imports: 6 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

View Source
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

type Counter struct {
	*atomic.Uint64
}

Counter is a single atomic uint64.

func New

func New() *Counter

New creates a pointer to a Counter.

func (*Counter) Decrement

func (c *Counter) Decrement() uint64

Decrement decreases the Counter value by one.

func (*Counter) Get

func (c *Counter) Get() uint64

Get returns the uint64 value.

func (*Counter) Increment

func (c *Counter) Increment() uint64

Increment increases the Counter value by one.

func (*Counter) Is

func (c *Counter) Is(value uint64) bool

Is compares the Counter with the given value and returns a boolean.

func (*Counter) Reset

func (c *Counter) Reset() uint64

Reset sets the value to 0 and returns the old value it replaced.

func (*Counter) Set

func (c *Counter) Set(value uint64) uint64

Set takes a value for the counter and returns the previous value.

type Counters

type Counters struct {
	// contains filtered or unexported fields
}

Counters contains multiple named atomic uint64 that are mutex protected.

func Load

func Load(path string) (*Counters, error)

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 NewCounters

func NewCounters() *Counters

NewCounters creates a pointer to Counters.

func (*Counters) Add

func (c *Counters) Add(name any, value uint64) error

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) Decrement

func (c *Counters) Decrement(name any) error

Decrement decreases the named Counter value by one.

func (*Counters) Delete

func (c *Counters) Delete(name any) error

Delete removes the named Counter from Counters.

func (*Counters) Get

func (c *Counters) Get(name any) (uint64, error)

Get returns the uint64 value for given name or an error if it does not exist.

func (*Counters) Increment

func (c *Counters) Increment(name any) error

Increment increases the named Counter value by one.

func (*Counters) Is

func (c *Counters) Is(name any, value uint64) bool

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.

func (*Counters) Reset

func (c *Counters) Reset(name any) uint64

Reset sets the value to 0 and returns the old value it replaced.

func (*Counters) Set

func (c *Counters) Set(name any, value uint64) uint64

Set takes a value for the named Counter and returns the previous value.

func (*Counters) Store

func (c *Counters) Store(path string) error

Store is a convenience function to save Counters to a file at the given path. Beware! File at path will be overwritten.

See Load(path string) (*Counters, error) for reading data.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL