Skip to content

0x726f6f6b6965/go-snowflake

Repository files navigation

go-snowflake

GoDoc Go Report Card codecov


About it

A simple Twitter snowflake sequence generator.

Futures

  • 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.

Format

+--------------------------------------------------------------------------+
| 1 Bit Unused | 41 Bit Timestamp |  8 Bit NodeID  |   14 Bit Sequence ID  |
+--------------------------------------------------------------------------+

Example

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())
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •