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 ¶
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
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) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (ID) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface. The ID is formatted according to RFC-4648 base32.
func (ID) String ¶
String returns implements the fmt.Stringer interface. Provided as a convenience wrapper over MarshalText.
func (*ID) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.