Documentation ¶
Overview ¶
Package uulid provides an UUID compatible ULID
Index ¶
- Constants
- Variables
- func MaxTime() (t time.Time)
- func Time(ms uint64) (t time.Time)
- func Timestamp(t time.Time) (ms uint64)
- type Generator
- type UULID
- func (id UULID) Compare(other UULID) (i int)
- func (id UULID) Entropy() (data []byte)
- func (id UULID) MarshalBinary() (data []byte, err error)
- func (id UULID) MarshalBinaryTo(dst []byte) (err error)
- func (id UULID) MarshalText() (data []byte, err error)
- func (id UULID) MarshalTextTo(dst []byte) (err error)
- func (id *UULID) Scan(src interface{}) (err error)
- func (id *UULID) SetEntropy(e []byte) (err error)
- func (id *UULID) SetTime(t time.Time) (err error)
- func (id *UULID) SetTimestamp(ms uint64) (err error)
- func (id *UULID) String() (s string)
- func (id UULID) Time() time.Time
- func (id UULID) Timestamp() uint64
- func (id *UULID) UnmarshalBinary(data []byte) (err error)
- func (id *UULID) UnmarshalText(data []byte) (err error)
- func (id UULID) Value() (v driver.Value, err error)
Constants ¶
const ( MaxTimestamp = 281474976710655 HexEncodedSize = 36 BinarySize = 16 )
Variables ¶
var ( // ErrBigTime is returned if the given time that is larger than MaxTimestamp. ErrBigTime = errors.New("uulid: time greater than supported by the ulid spec") // ErrDataSize is returned when parsing an invalid string representation of a UULID. ErrDataSize = errors.New("uulid: bad data size when parsing") // ErrBufferSize is returned when marshalling an UULID to a buffer < 36 bytes. ErrBufferSize = errors.New("uulid: bad buffer size when marshaling") // ErrInvalidType is returned when scan receives an invalid type. ErrInvalidType = errors.New("uulid: invalid type to unmarshal") // ErrMonotonicOverflow is returned if the current 10bit entropy overflows. ErrMonotonicOverflow = errors.New("uulid: monotonic overflow") // ErrSmallTime is returned if the current epoch time is lower than the previously seen // by the Generator. ErrSmallTime = errors.New("uulid: time is lower than current generator") )
Functions ¶
Types ¶
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator implements an UUID generator based on the ULID spec. The generated UULID is monotonically increased for calls within the same millisecond.
func NewGenerator ¶
NewGenerator is like NewGeneratorWithSeed() but uses a secure random seed from crypto/rand.
func NewGeneratorWithSeed ¶
NewGeneratorWithSeed creates a new UULID generator. It will used the given as the seed for the internal monotonic RNG.
Ensure that a good random seed is used or use NewGenerator() which provides a secure seed from crypto/rand.
type UULID ¶
type UULID [BinarySize]byte
An UULID is a 16 byte Universally Unique Lexicographically Sortable Identifier
The components are encoded as 16 octets. Each component is encoded with the MSB first (network byte order). 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 32_bit_uint_time_high | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 16_bit_uint_time_low | 16_bit_uint_random | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 32_bit_uint_random | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 32_bit_uint_random | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
func Parse ¶
Parse parses an encoded UULID, returning an error in case of failure.
ErrDataSize is returned if the length is different from an encoded UULID valid lengths, either 32 or 36 characters.
ErrBigTime is returned if time is greater than MaxTime().
func (UULID) Compare ¶
Compare returns an integer comparing id and other lexicographically. The result will be 0 if id==other, -1 if id < other, and +1 if id > other.
func (UULID) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface
func (UULID) MarshalBinaryTo ¶
MarshalBinaryTo writes the binary encoding of the ULID to the given buffer. ErrBufferSize is returned when the len(dst) != 16.
func (UULID) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (UULID) MarshalTextTo ¶
MarshalTextTo writes the UULID as a string to the given buffer. ErrBufferSize is returned when the len(dst) != EncodedSize.
func (*UULID) Scan ¶
Scan implements the sql.Scanner interface. It supports scanning a string or byte slice.
func (*UULID) SetEntropy ¶
SetEntropy sets the ULID entropy to the passed byte slice.
func (*UULID) SetTimestamp ¶
SetTimestamp sets the time component of the ULID to the given Unix time in milliseconds.
func (*UULID) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (*UULID) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.