snowflake

package module
v0.0.0-...-8150382 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2022 License: MIT Imports: 6 Imported by: 0

README

snowflake

Install

Set up your Go environment and run

go get github.com/GrokkingSystemDesign/snowflake

Usage

package main

import (
	"fmt"

	"github.com/GrokkingSystemDesign/snowflake"
)

func main() {
	m, err := snowflake.NewMachine(0)
	if err != nil {
		panic(err)
	}
	id, _ := m.Generate()
	fmt.Printf("Int64 ID: %d\n", id.Int64())
	fmt.Printf("String ID: %s\n", id.String())
	fmt.Printf("Base64 ID: %s\n", id.Base64())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Machine

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

Machine is the abstract representation of a real single machine, the ID generated by the same machine should be monotonically increasing, but all IDs generated from all machines will be trendily increasing

func NewMachine

func NewMachine(machineID int64) (*Machine, error)

New returns a machine that can be used to generate snowflake IDs

func (*Machine) Generate

func (m *Machine) Generate() (SnowflakeID, error)

Generate creates and returns a unique snowflake ID

type SnowflakeID

type SnowflakeID int64

SnowflakeID refer to https://en.wikipedia.org/wiki/Snowflake_ID +-----------------------------------------------------------------------------+ | 1 Bit Unused | 41 Bit Timestamp | 10 Bit MachineID | 12 Bit Sequence ID | +-----------------------------------------------------------------------------+

func ParseBase64

func ParseBase64(id string) (SnowflakeID, error)

func ParseBytes

func ParseBytes(id []byte) (SnowflakeID, error)

func ParseInt64

func ParseInt64(id int64) SnowflakeID

func (SnowflakeID) Base64

func (f SnowflakeID) Base64() string

func (SnowflakeID) Bytes

func (f SnowflakeID) Bytes() []byte

func (SnowflakeID) Int64

func (f SnowflakeID) Int64() int64

func (SnowflakeID) String

func (f SnowflakeID) String() string

Jump to

Keyboard shortcuts

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