A simple Twitter snowflake sequence generator.
- The lifecycle of the generator is 69 years.
- It can be leveraged on distributed systems.
- It can ensure the correct sequence order is on the one node strongly, but the correct sequence order is on the distributed systems weakly because of NTP.
+--------------------------------------------------------------------------+
| 1 Bit Unused | 41 Bit Timestamp | 8 Bit NodeID | 14 Bit Sequence ID |
+--------------------------------------------------------------------------+
package main
import (
"fmt"
"time"
"github.com/0x726f6f6b6965/go-snowflake"
)
func main() {
var (
startTime = time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
nodeID int64 = 3
)
// Create a new generator
generator, err := snowflake.NewGenerator(nodeID, startTime)
if err != nil {
fmt.Println(err)
return
}
// Generate a sequence.
sequence, _ := generator.Next()
// Print out the sequence.
fmt.Printf("Uint64 Sequence: %d\n", sequence.Uint64())
fmt.Printf("String Sequence: %s\n", sequence.String())
}