Documentation ¶
Overview ¶
Package snowflake implements methods to generate and unmarshal unique identifiers in the style of Twitters snowflake network ID tool.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*Snowflake)
Option is the interface for all Snowflake Options to follow in order to modify a new Snowflake via the NewWithOptions function.
func WithEpoch ¶
WithEpoch is a function to set a custom epoch for generating or parsing a Snowflake with a custom Epoch (such as Discord.)
Arguments:
e (uint64): The value of the epoch
Returns:
(Option): A Snowflake Option instance to be consumed by NewWithOptions.
func WithIncrimentID ¶
WithIncrimentID is a function to set a custom IncrimentID for generating a Snowflake with a specific IncrimentID.
This Option has no bearing on how an IncrimentID would be parsed by the Unmarshal function.
Arguments:
id (uint64): The value of the IncrimentID
Returns:
(Option): A Snowflake Option instance to be consumed by NewWithOptions.
func WithProcessID ¶
WithProcessID is a function to set a custom ProcessID for generating a Snowflake with a specific ProcessID.
This Option has no bearing on how a ProcessID would be parsed by the Unmarshal function.
Arguments:
id (uint64): The value of the ProcessID
Returns:
(Option): A Snowflake Option instance to be consumed by NewWithOptions.
func WithTime ¶
WithTime is a function to set a custom time for generating or parsing a Snowflake.
Arguments:
t (time.Time): The value of the custom time
Returns:
(Option): A Snowflake Option instance to be consumed by NewWithOptions.
func WithWorkerID ¶
WithWorkerID is a function to set a custom WorkerID for generating a Snowflake with a specific WorkerID.
This Option has no bearing on how a WorkerID would be parsed by the Unmarshal function.
Arguments:
id (uint64): The value of the WorkerID
Returns:
(Option): A Snowflake Option instance to be consumed by NewWithOptions.
type Snowflake ¶
type Snowflake struct { // Timestamp is used to generate the last 42 bits of the // snowflake. Timestamp time.Time // InternalWorkerID is used to identify the worker that // is generating this snowflake. This field makes up // the 17th - 21st bits of the snowflake. InternalWorkerID uint64 // InternalProcessID is used to identify the process of // the worker that is generating this snowflake. This // field makes up the 12th - 16th bits of the snowflake. InternalProcessID uint64 // IncrimentID is incremented each time a worker/process // combo creates another identifier. This field makes up // the first 11 bits of the snowflake. IncrimentID uint64 // contains filtered or unexported fields }
Snowflake is the core structure to held data used to create and unmarshal new snowflake identifiers.
func New ¶
func New() *Snowflake
New generates a new Snowflake with default values:
- The standard UNIX epoch
- The current time
- A WorkerID of 0
- A ProcessID of 0
- An IncrimentID of 0
Arguments:
None
Returns:
(*Snowflake): A pointer to the new instance of Snowflake.
func NewWithOptions ¶
NewWithOptions generates a new Snowflake with custom values, or uses the default value if an option is not specified. Default values are:
- The standard UNIX epoch
- The current time
- A WorkerID of 0
- A ProcessID of 0
- An IncrimentID of 0
Arguments:
None
Returns:
(*Snowflake): A pointer to the new instance of Snowflake.
func (Snowflake) Int ¶
Int returns the value of this snowflake as in int type.
Argumenets:
None
Returns:
(int): The int value of the snowflake.
func (Snowflake) Int64 ¶
Int64 returns the value of this snowflake as an int64 type.
Argumenets:
None
Returns:
(int64): The int64 value of the snowflake.
func (Snowflake) String ¶
String returns the string value of this snowflake.
Argumenets:
None
Returns:
(string): The string value of the snowflake.
func (Snowflake) Uint ¶
Uint returns the value of this snowflake as a uint type.
Argumenets:
None
Returns:
(uint): The uint value of the snowflake.