identifier

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: MIT Imports: 11 Imported by: 1

README

identifier

Very fast generator of unique IDs.

  • 8 bytes decoded integer
    • int64 (only 63 bits used)
    • contains a unix timestamp with millisecond precision
    • sequential
    • sortable by time
    • used in databases
  • 13 bytes encoded string
    • appears random
    • used in JSON

Install

go get github.com/webmafia/identifier

Usage

id := identifier.Generate()

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidId = errors.New("invalid ID")
)

Functions

This section is empty.

Types

type BinaryAppender

type BinaryAppender interface {
	// AppendText appends the binary representation of itself to the end of b
	// (allocating a larger slice if necessary) and returns the updated slice.
	//
	// Implementations must not retain b, nor mutate any bytes within b[:len(b)].
	AppendBinary([]byte) ([]byte, error)
}

BinaryAppender is the interface implemented by an object that can append the binary representation of itself. If a type implements both BinaryAppender and [BinaryMarshaler], then v.MarshalBinary() must be semantically identical to v.AppendBinary(nil).

type ID

type ID int64

func FromString added in v0.3.0

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

func Generate

func Generate() ID

Generates a unique ID. This is thread-safe.

Example
for range 10 {
	id := Generate()
	fmt.Println(id.Int64(), id)
}
Output:

TODO

func (ID) AppendBinary

func (id ID) AppendBinary(b []byte) ([]byte, error)

AppendBinary implements encoding.BinaryAppender.

func (ID) AppendText

func (id ID) AppendText(b []byte) ([]byte, error)

AppendText implements encoding.TextAppender.

func (ID) Int64

func (id ID) Int64() int64

func (ID) IsNil

func (id ID) IsNil() bool

func (ID) IsZero

func (id ID) IsZero() bool

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 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) (err error)

Scan implements sql.Scanner.

func (ID) String

func (id ID) String() string

String implements fmt.Stringer.

func (ID) Time

func (id ID) Time() time.Time

func (*ID) UnmarshalBinary

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

UnmarshalBinary implements encoding.BinaryUnmarshaler.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON implements json.Unmarshaler.

func (*ID) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (ID) Value

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

Value implements driver.Valuer.

type TextAppender

type TextAppender interface {
	// AppendText appends the textual representation of itself to the end of b
	// (allocating a larger slice if necessary) and returns the updated slice.
	//
	// Implementations must not retain b, nor mutate any bytes within b[:len(b)].
	AppendText(b []byte) ([]byte, error)
}

TextAppender is the interface implemented by an object that can append the textual representation of itself. If a type implements both TextAppender and [TextMarshaler], then v.MarshalText() must be semantically identical to v.AppendText(nil).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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