idgen

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2021 License: MIT Imports: 1 Imported by: 0

README

idgen

idgen only does 1 thing: generates unique uints. IDs may be released so they can be reused. It can provide "MaxUint" unique IDs.

Why did you make this?

I needed a way of generating a load of IDs for Windows Menus & MenuItems. They will sometimes need to be regenerated and thus the IDs would need recalculating. Whilst it was probably ok to just increment a counter, I needed a way to guarantee that some long running process wasn't going to hit that upper limit and screw up the application.

Installation

go get github.com/leaanthony/idgen

Usage

package main

import "github.com/leaanthony/idgen"

func main() {
	
    // Create a new generator
    generator := idgen.New()	
    
    // Get an ID
    id, err := generator.NewID()
    if err != nil {
    	// We have run out of available IDs
    }
    
    limited := generator.NewWithMaximum(1)

    // Get the only ID available
    id, err = limited.NewID()
    // err == nil
    
    // Get another when no more are available
    _, err = limited.NewID()
    // err != nil 
	
    // Release the id we had
    limited.ReleaseID(id)
    
    // Now we can get the ID again
    id, err = limited.NewID()
    // err == nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

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

Generator generates unique IDs (uint)

func New

func New() *Generator

New creates a new ID (uint) Generator

func NewWithMaximum

func NewWithMaximum(maximum uint) *Generator

NewWithMaximum creates a new ID (uint) Generator with a maximum number of unique IDs that may be generated

func (*Generator) NewID

func (g *Generator) NewID() (uint, error)

NewID returns an ID that has not been previously issued. Returns an error if all available IDs have been issued.

func (*Generator) ReleaseID

func (g *Generator) ReleaseID(ID uint)

ReleaseID frees up the given ID so that it may be reused

Jump to

Keyboard shortcuts

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