snowflake

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2025 License: MIT Imports: 4 Imported by: 2

Documentation

Overview

Package snowflake provides a very simple Twitter snowflake generator.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultEpoch is the default snowflake start timestamp epoch of 2020-01-01 00:00:00 UTC in milliseconds.
	DefaultEpoch int64 = 1577836800000
)

Functions

This section is empty.

Types

type ID

type ID struct {
	// contains filtered or unexported fields
}

An ID is a custom type used for a snowflake ID. This is used so we can attach methods onto the ID.

func (ID) Int64

func (i ID) Int64() int64

Int64 returns an int64 of the snowflake ID

func (ID) Node

func (i ID) Node() int64

Node returns an int64 of the snowflake ID node number

func (ID) Step

func (i ID) Step() int64

Step returns an int64 of the snowflake step (or sequence) number

func (ID) Time

func (i ID) Time() time.Time

Time returns the snowflake ID time

func (ID) UnixMilli

func (i ID) UnixMilli() int64

UnixMilli returns an int64 unix timestamp in milliseconds of the snowflake ID time

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node is a struct holds the basic information needed for a snowflake generator

func CustomNode

func CustomNode(epoch, node int64, nodeBits, stepBits int) *Node

CustomNode create a custom snowflake node.

func NewNode

func NewNode(node int64) *Node

NewNode returns a new snowflake node that can be used to generate snowflake ID. The default epoch: 2000-01-01 00:00:00 UTC The default ID format: +----------------------------------------------------------------------+ | 1 Bit Unused | 41 Bit Timestamp | 10 Bit NodeID | 12 Bit Sequence ID | +----------------------------------------------------------------------+ @see https://en.wikipedia.org/wiki/Snowflake_ID

func (*Node) Epoch

func (n *Node) Epoch() int64

Epoch return the epoch

func (*Node) LastID

func (n *Node) LastID() ID

LastID returns last createed snowflake ID

func (*Node) NextID

func (n *Node) NextID() ID

NextID creates and returns a unique snowflake ID To help guarantee uniqueness - Make sure your system is keeping accurate system time - Make sure you never have multiple nodes running with the same node ID

func (*Node) Node

func (n *Node) Node() int64

Node return the node

func (*Node) NodeBits

func (n *Node) NodeBits() int

NodeBits return node bits

func (*Node) StepBits

func (n *Node) StepBits() int

StepBits return step bits

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL