https://github.com/srahkmli/gosnowflake
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.
https://github.com/srahkmli/gosnowflake
go golang idgenerator snowflake snowflake-twitter
Last synced: 9 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/srahkmli/gosnowflake
- Owner: srahkmli
- License: mit
- Created: 2025-01-07T07:08:46.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-08T08:26:27.000Z (over 1 year ago)
- Last Synced: 2025-02-28T09:53:27.653Z (over 1 year ago)
- Topics: go, golang, idgenerator, snowflake, snowflake-twitter
- Language: Go
- Homepage: https://pkg.go.dev/github.com/srahkmli/gosnowflake
- Size: 7.81 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Snowflake
Snowflake is a Go package for generating unique IDs based on the Snowflake algorithm. It provides flexibility to configure custom epochs, node IDs, and sequence bit sizes, making it ideal for distributed systems requiring unique, high-throughput ID generation.
## Features
- **Thread-safe** ID generation.
- Configurable node and sequence bit sizes.
- Custom epoch for timestamp calculations.
- ID decomposition into timestamp, node ID, and sequence components.
- Optional base62-encoded IDs of specific lengths.
## Installation
Install the package using `go get`:
```sh
go get github.com/srahkmli/gosnowflake
```
## Usage
### Basic Example
```go
package main
import (
"fmt"
"log"
"time"
"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)
}
```
### Custom ID Length
Generate a base62-encoded ID with a specific length:
```go
customID, err := ss.GenerateCustomID(16)
if err != nil {
log.Fatalf("Failed to generate custom ID: %v", err)
}
fmt.Printf("Generated Custom ID: %s\n", customID)
```
### Decomposing an ID
Extract the timestamp, node ID, and sequence from a generated ID:
```go
timestamp, nodeID, sequence := ss.DecomposeID(id)
fmt.Printf("Timestamp: %d, Node ID: %d, Sequence: %d\n", timestamp, nodeID, sequence)
```
## Testing
Run the included tests to verify the functionality:
```sh
go test ./...
```
## Configuration
The `Config` struct allows for the following options:
- **Epoch**: Start time for ID generation in milliseconds.
- **NodeID**: Unique identifier for this generator instance.
- **NodeBits**: Number of bits allocated for the node ID.
- **SequenceBits**: Number of bits allocated for the sequence.
## License
This project is licensed under the MIT License. See the `LICENSE` file for details.