refcount

package
v0.0.0-...-c012b7a Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2021 License: Apache-2.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() {
	if r := recover(); 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