Snowflake - Simple twitter's snowflake uniquely identifiable descriptors (IDs) format generator for Go

Snowflake

Dead simple and fast Twitter's snowflake id generator in Go.

Installation

go get github.com/HotPotatoC/snowflake

Usage

  1. Generating a snowflake id
machineID := uint64(1)
sf := snowflake.New(machineID)

id := sf.NextID()
fmt.Println(id)
// 1292053924173320192

// or

id = snowflake.New(machineID).NextID()
fmt.Println(id)
// 1292053924173320192
  1. Parsing a snowflake id
parsed := snowflake.Parse(1292053924173320192)

fmt.Printf("Timestamp: %d\n", parsed.Timestamp)      // 1640942460724
fmt.Printf("Sequence: %d\n", parsed.Sequence)        // 0
fmt.Printf("Machine ID: %d\n", parsed.Discriminator) // 1
  1. Generating a snowflake ID with 2 discriminator fields
machineID := uint64(1)
processID := uint64(24)
sf := snowflake.New2(machineID, processID)

id := sf.NextID()
fmt.Println(id)
// 1292065108376162304

// or

id = snowflake.New2(machineID, processID).NextID()
fmt.Println(id)
// 1292065108376162304
  1. Parsing a snowflake id with 2 discriminator fields
parsed := snowflake.Parse2(1292065108376162304)

fmt.Printf("Timestamp: %d\n", parsed.Timestamp)       // 1640944495572
fmt.Printf("Sequence: %d\n", parsed.Sequence)         // 0
fmt.Printf("Machine ID: %d\n", parsed.Discriminator1) // 1
fmt.Printf("Process ID: %d\n", parsed.Discriminator2) // 24

Performance

Benched on Windows 10 - WSL 2 Ubuntu, Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz and 12GB of memory

goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkNewID
BenchmarkNewID/github.com/HotPotatoC/snowflake
BenchmarkNewID/github.com/HotPotatoC/snowflake-8          14012617         87.52 ns/op        0 B/op        0 allocs/op
BenchmarkNewID/github.com/bwmarrin/snowflake
BenchmarkNewID/github.com/bwmarrin/snowflake-8             4918552        244.3 ns/op        0 B/op        0 allocs/op
BenchmarkNewID/github.com/godruoyi/go-snowflake
BenchmarkNewID/github.com/godruoyi/go-snowflake-8          4916791        245.8 ns/op        0 B/op        0 allocs/op
PASS
ok   command-line-arguments 4.230s

Disclaimer: Benchmark results may be faster than other implementations. But I do not guarantee that this library is the safest snowflake id generator.

Support

Buy Me A Coffee

Owner
Juan Christian
he/him | 1st year Computer Science student
Juan Christian
Similar Resources

High performance unique number generator powered by Go

SEQSVR High performance unique number generator powered by Go 中文 README Features Distributed: Can be scaled horizontally High performance: Allocation

Nov 16, 2022

golang实现的分布式唯一ID生成器distributed id generator,有全局趋势递增、严防时钟漂移、高可用、高性能等特点

golang实现的分布式唯一ID生成器distributed id generator,有全局趋势递增、严防时钟漂移、高可用、高性能等特点

ekko-idgenerator是什么 顾名思义,ekko是一个分布式唯一ID生成器,参考了snowFlake思想,但是并不局限于其设计。 名称由来 英雄联盟的时间刺客ekko 特点 易用,最大限度保证系统的易用性,支持Get与MultiGet; 高并发,单机每秒100w个唯一ID生成; 高可用,理

Jan 4, 2023

A generator library for concise, unambiguous and URL-safe UUIDs

shortuuid A Go library that generates concise, unambiguous, URL-safe UUIDs. Based on and compatible with the Python library shortuuid. Often, one need

Jan 4, 2023

A simple uuid library based on RFC 4122

UUID generator A simple library that generates uuids. Supported versions: version 1 version 3 version 4 version 5 Supported variants: DCE Microsoft Th

Oct 20, 2021

Snowflake - A simple to use Go (golang) package to generate or parse Twitter snowflake IDs

Snowflake - A simple to use Go (golang) package to generate or parse Twitter snowflake IDs

❄️ Go-Snowflake A Snowflake Generator for Go A simple to use Go (golang) package

Oct 20, 2022

A simple to use Go (golang) package to generate or parse Twitter snowflake IDs

snowflake snowflake is a Go package that provides A very simple Twitter snowflake generator. Methods to parse existing snowflake IDs. Methods to conve

Dec 30, 2022

✨ Generate unique IDs (Port of Node package "generate-snowflake" to Golang)

✨ Generate Snowflake Generate unique IDs. Inspired by Twitter's Snowflake system. 📦 Installation Initialize your project (go mod init example.com/exa

Feb 11, 2022

Finds an identifiable hash value for each version of GitLab vulnerable to CVE-2021-22205

Finds an identifiable hash value for each version of GitLab vulnerable to CVE-2021-22205

Sep 20, 2022

Snowflake grafana datasource plugin allows Snowflake data to be visually represented in Grafana dashboards.

Snowflake grafana datasource plugin allows Snowflake data to be visually represented in Grafana dashboards.

Snowflake Grafana Data Source With the Snowflake plugin, you can visualize your Snowflake data in Grafana and build awesome chart. Get started with th

Dec 29, 2022

Snowflake - An implement of snowflake by go, use atomic instead of mutex

snowflake an implement of snowflake by go, use atomic instead of mutex 雪花算法的一种go

Feb 4, 2022

A work-in-progress Bitcoin wallet based on Output Descriptors

go-wallet A work-in-progress Bitcoin wallet Descriptors go-wallet is designed around Bitcoin Descriptors. It implements a Output Script Descriptors la

May 4, 2022

Project containing all scripts and descriptors to deploy Slurpanize in different ways

Slurpanize cloud infrastructure This project is built to install on any hardware or cloud infrastructure the Slurpanize platform. The installation is

Nov 24, 2021

A distributed unique ID generator inspired by Twitter's Snowflake

Sonyflake is a distributed unique ID generator inspired by Twitter's Snowflake.

Jan 2, 2023

❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

Dec 14, 2022

Read metrics from a Message Queue in Json format and expose them in a Prometheus compatible format

mq2prom Read metrics from a Message Queue in Json format and expose them in a Prometheus compatible format. Currently only works for MQTT compatible M

Jan 24, 2022

Using NFP (Number Format Parser) you can get an Abstract Syntax Tree (AST) from Excel number format expression

NFP (Number Format Parser) Using NFP (Number Format Parser) you can get an Abstract Syntax Tree (AST) from Excel number format expression. Installatio

Feb 4, 2022

ID type with marshalling to/from hash to prevent sending IDs to clients.

ID type with marshalling to/from hash to prevent sending IDs to clients.

Hide IDs Hide is a simple package to provide an ID type that is marshalled to/from a hash string. This prevents sending technical IDs to clients and c

Dec 10, 2022

Super short, fully unique, non-sequential and URL friendly Ids

Generator of unique non-sequential short Ids The package shortidenables the generation of short, fully unique, non-sequential and by default URL frien

Dec 30, 2022

Compact, sortable and fast unique IDs with embedded metadata.

Compact, sortable and fast unique IDs with embedded metadata.

A spec for unique IDs in distributed systems based on the Snowflake design, i.e. a coordination-based ID variant. It aims to be friendly to both machi

Dec 22, 2022
A simple to use Go (golang) package to generate or parse Twitter snowflake IDs

snowflake snowflake is a Go package that provides A very simple Twitter snowflake generator. Methods to parse existing snowflake IDs. Methods to conve

Dec 30, 2022
✨ Generate unique IDs (Port of Node package "generate-snowflake" to Golang)

✨ Generate Snowflake Generate unique IDs. Inspired by Twitter's Snowflake system. ?? Installation Initialize your project (go mod init example.com/exa

Feb 11, 2022
Snowflake - An implement of snowflake by go, use atomic instead of mutex

snowflake an implement of snowflake by go, use atomic instead of mutex 雪花算法的一种go

Feb 4, 2022
❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).
❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

Dec 14, 2022
Compact, sortable and fast unique IDs with embedded metadata.
Compact, sortable and fast unique IDs with embedded metadata.

A spec for unique IDs in distributed systems based on the Snowflake design, i.e. a coordination-based ID variant. It aims to be friendly to both machi

Dec 22, 2022
K-Sortable Globally Unique IDs

ksuid ksuid is an efficient, comprehensive, battle-tested Go library for generating and parsing a specific kind of globally unique identifier called a

Jan 9, 2023
Golang wrapper for the Snowflake Api.

GoSnowflakeApi GoSnowflakeApi is an api wrapper for the snowflake api written in golang for golang developers. Example package main import ( "fmt

Jul 25, 2021
Snowflake implemented in GO (Golang)

snowflake Snowflake is a fast, goroutine-safe unique ID generator built for distributed systems Key concepts Snowflake Snowflakes are int64s. uint64 i

Oct 27, 2022
A network service for generating unique ID numbers inspired by Twitter's Snowflake.

Hanabira Hanabira is a network service for generating unique ID numbers inspired by Twitter's Snowflake. How to run hanabira-cluster and etcd-cluster

Jan 13, 2022
A tiny and fast Go unique string generator

Nano ID A tiny and fast Go unique string generator Safe. It uses cryptographically strong random APIs and tests distribution of symbols. Compact. It u

Nov 11, 2022