Home

Awesome

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

<a href="https://www.buymeacoffee.com/hotpotato" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>