idpool

package module
v0.0.0-...-e17266e Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 1 Imported by: 0

README

go-idpool

Basic id pool implementation. Unique ids can be pulled from and released back into the pool as needed.
Useful for tagging things with a life cycle.

Example
package main

import (
	"fmt"

	idp "github.com/franklange/go-idpool"
)

func main() {
	p := idp.Idpool{}

	n0 := p.Next()
	fmt.Println(n0) // 0

	n1 := p.Next()
	fmt.Println(n1) // 1

	n2 := p.Next()
	fmt.Println(n2) // 2

	ok := p.Release(n1)
	fmt.Println(ok) // true, release 1 back into the id pool

	n3 := p.Next()
	fmt.Println(n3) // 1, lowest free id in the pool after previous release

	nok := p.Release(99)
	fmt.Println(nok) // false, can't release ids that haven't been given out

	p.SetStart(100) // set first id to be pulled (resets pool)

	n100 := p.Next()
	fmt.Println(n100) // 100
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Idpool

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

func (*Idpool) Next

func (p *Idpool) Next() uint

func (*Idpool) Release

func (p *Idpool) Release(n uint) bool

func (*Idpool) SetStart

func (p *Idpool) SetStart(s uint)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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