refcount

package
v0.0.0-...-4df9163 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: AGPL-3.0 Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decrement

func Decrement(refCounter string, value uint) uint

Decrement decreases a refCounter by the value. If the ref counter doesn't exist, a new one is created. The counter's new value is returned. A counter cannot be decreased below zero.

Example
refCounter1 := "testdec1"
fmt.Println(Increment(refCounter1, 10))
fmt.Println(Decrement(refCounter1, 1))
fmt.Println(Decrement(refCounter1, 2))

refCounter2 := "testdec2"
fmt.Println(Decrement(refCounter2, 1))
fmt.Println(Increment(refCounter2, 1))
fmt.Println(Decrement(refCounter2, 1))
fmt.Println(Decrement(refCounter2, 1))
Output:

10
9
7
0
1
0
0

func Get

func Get(refCounter string) uint

Get returns the current value of the refCounter.

func Increment

func Increment(refCounter string, value uint) uint

Increment increases a refCounter by the value. If the ref counter doesn't exist, a new one is created. The counter's new value is returned.

Example
refCounter1 := "testinc1"
fmt.Println(Increment(refCounter1, 1))
fmt.Println(Increment(refCounter1, 1))
fmt.Println(Increment(refCounter1, 2))

refCounter2 := "testinc2"
fmt.Println(Increment(refCounter2, 1))
fmt.Println(Increment(refCounter2, 10))

// Test overflow panic.
defer func() {
	r := recover()
	if r != nil {
		fmt.Printf("Recovered: %v\n", r)
	}
}()
var maxUint uint = ^uint(0)
fmt.Println(Increment(refCounter2, maxUint))
Output:

1
2
4
1
11
Recovered: Ref counter "testinc2" overflowed from 11 to 10

Types

This section is empty.

Jump to

Keyboard shortcuts

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