package ksuidimport ()// Sequence is a KSUID generator which produces a sequence of ordered KSUIDs// from a seed.//// Up to 65536 KSUIDs can be generated by for a single seed.//// A typical usage of a Sequence looks like this://// seq := ksuid.Sequence{// Seed: ksuid.New(),// }// id, err := seq.Next()//// Sequence values are not safe to use concurrently from multiple goroutines.typeSequencestruct {// The seed is used as base for the KSUID generator, all generated KSUIDs // share the same leading 18 bytes of the seed. Seed KSUID count uint32// uint32 for overflow, only 2 bytes are used}// Next produces the next KSUID in the sequence, or returns an error if the// sequence has been exhausted.func ( *Sequence) () (KSUID, error) { := .Seed// copy := .countif > math.MaxUint16 {returnNil, errors.New("too many IDs were generated") } .count++returnwithSequenceNumber(, uint16()), nil}// Bounds returns the inclusive min and max bounds of the KSUIDs that may be// generated by the sequence. If all ids have been generated already then the// returned min value is equal to the max.func ( *Sequence) () ( KSUID, KSUID) { := .countif > math.MaxUint16 { = math.MaxUint16 }returnwithSequenceNumber(.Seed, uint16()), withSequenceNumber(.Seed, math.MaxUint16)}func withSequenceNumber( KSUID, uint16) KSUID {binary.BigEndian.PutUint16([len()-2:], )return}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.