snowflake

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2021 License: MIT Imports: 3 Imported by: 0

README

Distributed global unique ID generator (Snowflake)

Use the golang to implement Snowflake global unique ID generator from twitter. The unit64 type number split this 64 bits number to different part. 1 bit is keeping for future use 41 bits for the timestamp, use the unix time in this project 5 bits for the data center ID 5 bits for the machine ID 12 bits for the machine serial Number which will reset in 1 millisecond. Use the above feature to make sure each ID generater from the different machine could be unique.

Install

go get github.com/Noahnut/snowflake

Example Usage

package main

import (
	"fmt"

	"github.com/Noahnut/snowflake"
)

func main() {
    // Create the new snow flake
	s := NewSnowflakeUniqueGenerator(1, 1)

    //Start the Snowflake
	s.StartSnowflake()
	defer s.StopSnowflake()

	for i := 0; i < 70; i++ {

        //get the ID from the snowflake
		ID := s.GetUniqueID()
		fmt.Println(strconv.FormatUint(ID, 2))
		time.Sleep(time.Microsecond)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Snowflake

type Snowflake struct {
	// contains filtered or unexported fields
}

func NewSnowflakeUniqueGenerator

func NewSnowflakeUniqueGenerator(machineID int, datacenterID int) *Snowflake

func (*Snowflake) GetUniqueID

func (s *Snowflake) GetUniqueID() uint64

func (*Snowflake) StartSnowflake

func (s *Snowflake) StartSnowflake()

func (*Snowflake) StopSnowflake

func (s *Snowflake) StopSnowflake()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL