guid

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2021 License: BSD-3-Clause, MIT Imports: 7 Imported by: 0

Documentation

Overview

Package guid facilitates the creation of Globally Unique Identifiers (GUIDs).

Index

Examples

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
}

A Generator is a source of IDs.

func NewGenerator

func NewGenerator(src rand.Source) *Generator

NewGenerator creates a new Generator from a random Source. The uniqueness of an ID is a function of the quality of its Source. A PRNG should have, at minimum, a period of 2^128. For security

func (*Generator) ID

func (g *Generator) ID() ID

ID returns a newly created ID.

type ID

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

ID represents a globally unique identifier (GUID).

An ID is composed of 128 random bits. Assuming no implementation problems with the random number generator used it its construction, the probability of two identical IDs existing can be computed as a birthday problem:

p(n; m) ~= 1 - ((m - 1)/m)^((n/2)*(n-1))

Where n is the number of IDs in existence and m is 2^128, representing the total number of possible IDs.

Thus, for even a 1% probability of collision, 2.6 quintillion IDs would need to exist.

var Zero ID

Zero is the zero value for an ID.

func New

func New() ID

New creates a new ID using the default generator.

The default generator has good statistical properties, but is not suited for security-sensitive usage. For such applications, use NewGenerator with a a cryptographically secure pseudorandom number generator to generate IDs.

Example
package main

import (
	"fmt"

	"bytelog.org/pkg/guid"
	"golang.org/x/exp/rand"
)

func main() {
	gen := guid.NewGenerator(rand.NewSource(0))
	id := gen.ID()

	fmt.Println(id)

}
Output:

PFSOICAI7L7EUJCON4DOQDB8E0

func (ID) IsZero

func (id ID) IsZero() bool

IsZero reports whether id represents the zero ID.

func (ID) MarshalBinary

func (id ID) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (ID) MarshalText

func (id ID) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The ID is formatted according to RFC-4648 base32.

func (ID) String

func (id ID) String() string

String returns implements the fmt.Stringer interface. Provided as a convenience wrapper over MarshalText.

func (*ID) UnmarshalBinary

func (id *ID) UnmarshalBinary(b []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

func (*ID) UnmarshalText

func (id *ID) UnmarshalText(b []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The ID is expected to be in RFC-4648 base32 format.

Jump to

Keyboard shortcuts

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