Dead simple and fast Twitter's snowflake id generator in Go.
go get github.com/HotPotatoC/snowflake
- Generating a snowflake id
machineID := uint64(1)
sf := snowflake.New(machineID)
id := sf.NextID()
fmt.Println(id)
// 1292053924173320192
- 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
- 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
- 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