Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ID ¶
type ID interface { // IsZero returns true if id is zero value. IsZero() bool // Parent returns the direct parent of id. // If id is zero or root, then parent is zero. Parent() ID // Index returns the index of id in the parant. // The index of root id is 0. Index() (index uint64) // Child returns the derived child id according to the given index. Child(index uint64) (child ID) // IsRoot returns true if id is a root id. IsRoot() bool // Root returns the root of id. Root() (root ID) // Chain returns the id derivation chain. // The order of chain from root to id. // chain := []ID{root, ..., parent, id} Chain() (chain []ID) // Deep returns the number of parents in the id derivation chain. // The deep of root id is 0. Deep() (deep int) // Equal reports whether id and xid represent the same id. Equal(xid ID) bool // String returns string expression of id. String() string }
ID is a self-describing identification interface.
func NewRandom ¶
NewRandom creates an new random root id based on crypto/rand.Reader. If err is not nil, root is nil.
func NewRandomFromReader ¶
NewRandom creates an new random root id based on given reader. If err is not nil, root is nil.
func ParseRandom ¶
ParseRandom decodes hex encoded sid into an id.
Example ¶
id := Must(ParseRandom("a9e272b77827c32a785e27342acfd1680a1f8001")) p := fmt.Println p("ID:", id) p("Chain:", id.Chain()) p("IsZero:", id.IsZero()) p("Parent:", id.Parent()) p("Index:", id.Index()) p("Child(10):", id.Child(10)) p("IsRoot:", id.IsRoot()) p("Root:", id.Root()) p("Deep:", id.Deep()) p("Equal(NewRandom()):", id.Equal(Must(NewRandom())))
Output: ID: a9e272b77827c32a785e27342acfd1680a1f8001 Chain: [a9e272b77827c32a785e27342acfd168 a9e272b77827c32a785e27342acfd1680a a9e272b77827c32a785e27342acfd1680a1f a9e272b77827c32a785e27342acfd1680a1f8001] IsZero: false Parent: a9e272b77827c32a785e27342acfd1680a1f Index: 128 Child(10): a9e272b77827c32a785e27342acfd1680a1f80010a IsRoot: false Root: a9e272b77827c32a785e27342acfd168 Deep: 3 Equal(NewRandom()): false
type Seq ¶
type Seq struct {
// contains filtered or unexported fields
}
Seq is a sequence number generator.
All public methods can be safely called concurrently.
Example ¶
seq := NewSeq() p := fmt.Println p(seq.Next()) root := Must(NewRandom()) id := NextChild(root, seq) p(id.Index())
Output: 0 1
type SeqOption ¶
type SeqOption func(seq *Seq)
func SeqIncrementBy ¶
SeqIncrementBy specifies which value is added to the current sequence value to create an new value. A positive value will make an ascending sequence, a negative one a descending sequence. The default value is 1.
func SeqStartWith ¶
SeqStartWith allows the sequence to begin anywhere. The default starting value is 0.