HotPotatoC / snowflake

❄️ Simple twitter's snowflake uniquely identifiable descriptors (IDs) format generator for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Snowflake

GitHub go.mod Go version of a Go module GoDoc reference example GoReportCard example GitHub release GitHub license codecov

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
  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.Field) // 1
  1. Generating a snowflake ID with 2 field fields
machineID := uint64(1)
processID := uint64(24)
sf := snowflake.New2(machineID, processID)

id := sf.NextID()
fmt.Println(id)
// 1292065108376162304
  1. Parsing a snowflake id with 2 field 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.Field1) // 1
fmt.Printf("Process ID: %d\n", parsed.Field2) // 24

Support

Buy Me A Coffee

About

❄️ Simple twitter's snowflake uniquely identifiable descriptors (IDs) format generator for Go

License:MIT License


Languages

Language:Go 100.0%