idgen

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT, Apache-2.0 Imports: 14 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// Epoch is set to the twitter snowflake epoch of Nov 04 2010 01:42:54 UTC in milliseconds
	// You may customize this to set a different epoch for your application.
	Epoch int64 = 1288834974657
	// NodeBits holds the number of bits to use for Snowflake
	// Remember, you have a total 22 bits to share between Snowflake/Step
	NodeBits uint8 = 10

	// StepBits holds the number of bits to use for Step
	// Remember, you have a total 22 bits to share between Snowflake/Step
	StepBits uint8 = 12
)

Variables

This section is empty.

Functions

func NewOrderedID

func NewOrderedID() uint64

单机顺序id

func NewOrderedIDGenerator added in v1.7.13

func NewOrderedIDGenerator(initial uint64) func() uint64

func NewRandomIDGenerator added in v1.7.13

func NewRandomIDGenerator(randSource *rand.Rand) *randomIDGenerator

Types

type ID added in v1.7.13

type ID []byte

func NewRandomID

func NewRandomID() ID

func UniqueID

func UniqueID() ID

func (ID) Base32 added in v1.7.17

func (t ID) Base32() string

func (ID) Base58 added in v1.7.17

func (t ID) Base58() string

func (ID) Base62 added in v1.7.17

func (t ID) Base62() string

func (ID) Base64 added in v1.7.17

func (t ID) Base64() string

func (ID) Bytes added in v1.7.17

func (t ID) Bytes() []byte

func (ID) Hex added in v1.7.17

func (t ID) Hex() string

func (ID) IsValid added in v1.7.13

func (t ID) IsValid() bool

IsValid checks whether the trace TraceID is valid. A valid trace ID does not consist of zeros only.

func (ID) MarshalJSON added in v1.7.13

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

MarshalJSON implements a custom marshal function to encode ID as a hex string.

func (ID) String added in v1.7.13

func (t ID) String() string

String returns the hex string representation form of a TraceID.

func (ID) UnmarshalJSON added in v1.7.13

func (t ID) UnmarshalJSON(data []byte) error

type JSONSyntaxError added in v1.7.14

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

A JSONSyntaxError is returned from UnmarshalJSON if an invalid ID is provided.

func (JSONSyntaxError) Error added in v1.7.14

func (j JSONSyntaxError) Error() string

type Snowflake added in v1.7.14

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

A Snowflake struct holds the basic information needed for a snowflake generator node

func NewSnowflake added in v1.7.14

func NewSnowflake(node uint16, nodeBits uint8) *Snowflake

NewSnowflake returns a new snowflake node that can be used to generate snowflake IDs

func (*Snowflake) Decompose added in v1.7.14

func (n *Snowflake) Decompose(id uint64) (timestamp int64, nodeId uint16, step uint16)

func (*Snowflake) Epoch added in v1.7.14

func (n *Snowflake) Epoch(t time.Time) *Snowflake

func (*Snowflake) Generate added in v1.7.14

func (n *Snowflake) Generate() uint64

Generate creates and returns a unique snowflake ID To help guarantee uniqueness - Make sure your system is keeping accurate system time - Make sure you never have multiple nodes running with the same node ID

Jump to

Keyboard shortcuts

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