naruepanart / snowflakeid

The Snowflake ID Generator is a Go package that allows you to generate unique and distributed IDs based on a combination of timestamp, machine ID, and sequence number.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Snowflake ID Generator

The Snowflake ID Generator is a Go package that allows you to generate unique and distributed IDs based on a combination of timestamp, machine ID, and sequence number.

Installation

To use the Snowflake ID Generator in your Go project, you can simply install it using the go get command:

go get -u github.com/naruepanart/snowflakeid

Usage

To use the Snowflake ID Generator in your Go application, follow these steps:

  1. Import the package:
import (
	"fmt"
	"github.com/naruepanart/snowflakeid"
)
  1. Create a Snowflake instance with a unique machine ID:
machineID := 1 // Set the machine ID according to the actual scenario
sf := snowflakeid.NewSnowflake(int64(machineID))
  1. Generate unique IDs using the GenerateID method:
for i := 0; i < 10; i++ {
    id := sf.GenerateID()
    fmt.Println(id)
}

Example

Here's a complete example of using the Snowflake ID Generator in a Go application:

package main

import (
	"fmt"
	"github.com/naruepanart/snowflakeid"
)

func main() {
	machineID := 1 // Set the machine ID according to the actual scenario
	sf := snowflakeid.NewSnowflake(int64(machineID))

	// Generate and print 10 unique IDs
	for i := 0; i < 10; i++ {
		id := sf.GenerateID()
		fmt.Println(id)
	}
}

Adjust the machine ID based on your deployment environment.

Contributing

If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.

About

The Snowflake ID Generator is a Go package that allows you to generate unique and distributed IDs based on a combination of timestamp, machine ID, and sequence number.

License:MIT License


Languages

Language:Go 100.0%