snowflake

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2025 License: MIT Imports: 7 Imported by: 0

README

snowflake

high performance Snowflake library built in sql/driver and json support

installation

go get github.com/acelikesghosts/snowflake

usage

package main

import (
    "log"
    "github.com/acelikesghosts/snowflake"
)

// example: Twitter's (this also exists at snowflake.TwitterEpoch)
const myTimestampOffset = 1288834974657

func main() {
    nodeId := 1
    epochInMillis := snowflake.TwitterEpoch
    
    generator, _ := snowflake.NewGenerator(nodeId, epochInMillis)

    snowflake := generator.MustGenerate()
    log.Printf("snowflake: %s", snowflake.String())

    // unix millis
    timestamp := snowflake.GetTimestampRaw() + myTimestampOffset
    seq := snowflake.GetSeq()
    nodeId := snowflake.GetNodeId()

    log.Printf("timestamp (unix millis): %d\nseq: %d\nnode id: %d", timestamp, seq, nodeId)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TwitterEpoch int64 = 1288834974657
	DiscordEpoch int64 = 1420070400000
)

presets for ease of use with other services.

View Source
var (
	ErrInvalidNodeId           = errors.New("node ID exceeds maximum of 10 bits")
	ErrGenerationClockRollback = errors.New("clock went backwards")
)
View Source
var ErrInvalidSnowflake = errors.New("invalid snowflake")

Functions

This section is empty.

Types

type Snowflake

type Snowflake int64

func ParseSnowflake

func ParseSnowflake(value int64) Snowflake

func ParseStringSnowflake

func ParseStringSnowflake(str string) (Snowflake, error)

func (Snowflake) GetNodeId

func (s Snowflake) GetNodeId() (nodeId int64)

Gets the node id of the Snowflake

func (Snowflake) GetSeq

func (s Snowflake) GetSeq() (seq int64)

Gets the sequence number of the Snowflake.

func (Snowflake) GetTimestampRaw

func (s Snowflake) GetTimestampRaw() (unixMillis int64)

Gets the raw timestamp (in unix millis) of the Snowflake You will have to manually add the timestamp offset to this value.

func (Snowflake) MarshalJSON

func (s Snowflake) MarshalJSON() ([]byte, error)

func (*Snowflake) Scan

func (s *Snowflake) Scan(src any) error

func (Snowflake) String

func (s Snowflake) String() string

func (*Snowflake) UnmarshalJSON

func (s *Snowflake) UnmarshalJSON(data []byte) error

func (Snowflake) Value

func (s Snowflake) Value() (driver.Value, error)

type SnowflakeGenerator

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

func NewGenerator

func NewGenerator(nodeId, timestampEpoch int64) (*SnowflakeGenerator, error)

func (*SnowflakeGenerator) Generate

func (s *SnowflakeGenerator) Generate() (Snowflake, error)

Generates a Snowflake.

func (*SnowflakeGenerator) MustGenerate

func (s *SnowflakeGenerator) MustGenerate() Snowflake

MustGenerate panics on error

Jump to

Keyboard shortcuts

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