Skip to content

Go package to generate and parse unique IDs based on Twitter's Snowflake algorithm.

License

Notifications You must be signed in to change notification settings

smnvdev/snowflake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snowflake IDs Generator

godoc.org wiki

Go package to generate and parse unique IDs based on Twitter's Snowflake algorithm.

Getting Started

Installing

go get github.com/smnvdev/snowflake

Simple Usage

package main

import (
    "fmt"
    "github.com/smnvdev/snowflake"
)

func main() {
    machineID := 1
    g, err := snowflake.NewGenerator(machineID)
	if err != nil {
		fmt.Println(err)
		return
	}

    id := g.Next()
    fmt.Println(id)
}

Parse ID

You can parse the ID to get the timestamp, machine ID, and sequence number using the Parse function of the generator.

Custom Epoch

This package uses the Twitter Epoch of 1288834974657 or Nov 04 2010 01:42:54 by default. You can set your own epoch value by setting snowflake.SetEpochTime before creating a new generator.

Custom Machine ID

You can set your own machine ID by setting snowflake.SetMachineID before creating a new generator.

Custom Sequence Bits

You can set your own sequence bits by setting snowflake.SetSequenceBits before creating a new generator.

About

Go package to generate and parse unique IDs based on Twitter's Snowflake algorithm.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages