Snowflake v1.0.0 - First Release 🎉
I am excited to announce the first release of Snowflake, a Go package for generating unique IDs based on the Snowflake algorithm. This release lays the foundation for high-throughput and distributed ID generation with a range of configurable options.
Highlights
-
Thread-Safe ID Generation
Ensure safe and efficient ID generation in concurrent environments. -
Custom Configuration
Flexible options for:- Epoch
- Node ID and size
- Sequence bit size
-
ID Decomposition
Easily extract timestamp, node ID, and sequence components from generated IDs. -
Custom Base62 Encoded IDs
Generate readable IDs of specific lengths.
Installation
Install the package using go get:
go get github.com/srahkmli/gosnowflakeGetting Started
Basic Example
Generate unique IDs with minimal setup:
package main
import (
"fmt"
"log"
"time"
snowflake "github.com/srahkmli/gosnowflake"
)
func main() {
cfg := snowflake.Config{
Epoch: time.Now().Add(-time.Hour).UnixMilli(),
NodeID: 1,
NodeBits: 10,
SequenceBits: 12,
}
ss, err := snowflake.NewSnowFlake(cfg)
if err != nil {
log.Fatalf("Failed to initialize Snowflake: %v", err)
}
id, err := ss.GenerateID()
if err != nil {
log.Fatalf("Failed to generate ID: %v", err)
}
fmt.Printf("Generated ID: %d\n", id)
}Decomposing an ID
Break down a generated ID into its components:
timestamp, nodeID, sequence := ss.DecomposeID(id)
fmt.Printf("Timestamp: %d, Node ID: %d, Sequence: %d\n", timestamp, nodeID, sequence)Testing
Run the comprehensive test suite to validate functionality:
go test ./...What's Next?
This is just the beginning! We’re looking forward to:
- Community feedback
- New feature suggestions
- Contributions to enhance Snowflake
License
This project is licensed under the MIT License. See the LICENSE file for details.
Thank you for trying Snowflake! 🚀