sole

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2023 License: MIT Imports: 10 Imported by: 0

README

UID - Sortable, human friendly and lock free unique id library

  1. 4 byte unix timstamp in seconds - with custom epoch
  2. 4 byte counter - to make sure of uniqueness
  3. 2 byte random - prevent user from accidentally typing other ppl ids

Custom base32 charset with ambigues characters removed

Base32 charset: 0123456789ACDEFGHJKLMNPQRTUVWXYZ

Ambigues characters

  • O -> 0
  • I -> 1
  • S -> 5
  • B -> 8

Contribution:

Loop unrooling optimization for base32 encode and decode are inpired by solutionroute/rid

Documentation

Index

Constants

View Source
const (

	// Unix timestamp of Nov 04 2010 01:42:54 UTC in seconds,
	// you may customize this to set a different epoch for your application.
	SnowflakeEpoch int64 = 1288834974
)

Variables

View Source
var (
	ErrInvalidStringLength = errors.New("ErrInvalidStringLength")
	ErrInvalidStringChar   = errors.New("ErrInvalidStringChar")
)

Functions

This section is empty.

Types

type Generator

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

func New

func New(epoch int64, sequence uint32) Generator

func (*Generator) Extract

func (gen *Generator) Extract(id ID) (time.Time, uint32, [2]byte)

func (*Generator) NewID

func (gen *Generator) NewID() (id ID)

type ID

type ID [lenbyt]byte
var Empty ID

func Parse

func Parse(str string) (id ID, err error)

func (ID) MarshalBinary

func (id ID) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler

func (ID) MarshalText

func (id ID) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler

func (*ID) Scan

func (id *ID) Scan(src any) error

Scan implements database/sql.Scanner

func (ID) String

func (id ID) String() string

String implements fmt.Stringer

func (*ID) UnmarshalBinary

func (id *ID) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(p []byte) error

UnmarshalJSON implements encoding/json.Unmarshaler

func (*ID) UnmarshalText

func (id *ID) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler

func (ID) Value

func (id ID) Value() (driver.Value, error)

Value implements database/sql/driver.Valuer

Jump to

Keyboard shortcuts

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