Documentation ¶
Index ¶
- Constants
- func EncodeDescending(src string) string
- func SetMachineId(ID uint64)
- func SetMachineIdHost(addr net.IP, port uint16)
- type Id
- func (id Id) MarshalBinary() ([]byte, error)
- func (id Id) MarshalBinaryTo(dst []byte) error
- func (id Id) MarshalText() ([]byte, error)
- func (id Id) MarshalTextTo(dst []byte) error
- func (id Id) String() string
- func (id *Id) UnmarshalBinary(data []byte) error
- func (id *Id) UnmarshalText(v []byte) error
Constants ¶
const EncodedSize = 26
EncodedSize is the length of a text encoded Id.
const Encoding = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
Encoding is the base 32 encoding alphabet used in Id strings.
Variables ¶
This section is empty.
Functions ¶
func EncodeDescending ¶
EncodeDescending returns a lexicographically sortable descending encoding of a given id, e.g. 000 -> ZZZ, which allows reversing the sort order when stored contiguously since ids are lexicographically sortable. The returned string will be of len(src), and assumes src is from the base32 crockford alphabet, otherwise using 0xFF.
func SetMachineId ¶
func SetMachineId(ID uint64)
SetMachineId may only be called by one thread before any id generation is done. It must be set if multiple machines are generating ids in order to avoid collisions. Only the least significant 48 bits are used.
func SetMachineIdHost ¶
SetMachineIdHost is a convenience wrapper to hide bit twiddling of calling SetMachineId, it has the same constraints as SetMachineId with an addition that net.IP must be a ipv4 address.
Types ¶
type Id ¶
type Id [16]byte
func New ¶
func New() Id
New will generate a new Id for use. New is safe to be called from concurrent threads. SetMachineId should be called once before any calls to New are made. 2^32 calls to New per millisecond will be unique, provided machine id is seeded correctly across machines.
binary format: [ [ 48 bits time ] [ 48 bits machineID ] [ 32 bits counter ] ]
Ids are sortable within (not between, thanks to clocks) each machine, with a modified base32 encoding exposed for convenience in API usage.
func NewWithTime ¶
NewWithTime returns an id that uses the milliseconds from the given time. New is identical to NewWithTime(time.Now())
func (Id) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface by returning the Id as a byte slice.
func (Id) MarshalBinaryTo ¶
MarshalBinaryTo writes the binary encoding of the Id to the given buffer. ErrBufferSize is returned when the len(dst) != 16.
func (Id) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface by returning the string encoded Id.
func (Id) MarshalTextTo ¶
MarshalTextTo writes the Id as a string to the given buffer. an error is returned when the len(dst) != 26.
func (Id) String ¶
String returns a lexicographically sortable string encoded Id (26 characters, non-standard base 32) e.g. 01AN4Z07BY79KA1307SR9X4MV3 Format: ttttttttttmmmmmmmmmmeeeeee where t is time, m is machine id and c is a counter
func (*Id) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface by copying the passed data and converting it to an Id. ErrDataSize is returned if the data length is different from Id length.
func (*Id) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface by parsing the data as string encoded Id.
an error is returned if the len(v) is different from an encoded Id's length. Invalid encodings produce undefined Ids.