snowflake

package
v2.4.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package snowflake provides a very simple Twitter snowflake generator and parser.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ID

type ID int64

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

func ParseBase2

func ParseBase2(id string) (ID, error)

ParseBase2 converts a Base2 string into a snowflake ID

func ParseInt64

func ParseInt64(id int64) ID

ParseInt64 converts an int64 into a snowflake ID

func ParseString

func ParseString(id string) (ID, error)

ParseString converts a string into a snowflake ID

func (*ID) Base2

func (id *ID) Base2() string

Base2 returns a string base2 of the snowflake ID

Example
id := gN.Generate()
log.Println(id.Base2())
Output:

func (*ID) Int64

func (id *ID) Int64() int64

Int64 returns an int64 of the snowflake ID

func (*ID) Node

func (id *ID) Node(maskNode int64, shiftNode uint8) int64

Node returns an int64 of the snowflake ID node number

func (*ID) Step

func (id *ID) Step(stepMask int64) int64

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

func (*ID) String

func (id *ID) String() string

String returns a string of the snowflake ID

Example
id := gN.Generate()
log.Println(id.String())
log.Println(id.Step(1024))
Output:

func (*ID) Time

func (id *ID) Time(baseTime time.Time, shiftTime uint8) time.Time

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

type Node

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

A Node struct holds the basic information needed for a snowflake generator time[node][step]

func NewNode

func NewNode(psw int64, baseTime time.Time, numNodeBits, numStepBits uint8) (*Node, error)

NewNode returns a new snowflake node that can be used to generate snowflake IDs

func (*Node) BaseTime

func (n *Node) BaseTime() time.Time

func (*Node) Generate

func (n *Node) Generate() ID
Example
// BaseT := time.Date(2022, 7, 1, 16, 10, 54, 0, time.UTC)
n, err := NewNode(0, BaseT, 10, 12)
if err != nil {
	panic(err)
}
n.Generate()
// ...
n.Generate()

log.Println(n.MaskStep())
Output:

func (*Node) MaskNode

func (n *Node) MaskNode() int64

func (*Node) MaskStep

func (n *Node) MaskStep() int64

func (*Node) ShiftNode

func (n *Node) ShiftNode() uint8

func (*Node) ShiftTime

func (n *Node) ShiftTime() uint8

Jump to

Keyboard shortcuts

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