minid

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

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

Go to latest
Published: Nov 21, 2025 License: MIT Imports: 7 Imported by: 0

README

minid

godoc license

A reasonably fast, (optionally) somewhat sortable, URL-safe and human-friendly ID generator for Go, which can be used either as a CLI tool or a library.

Generate fully random IDs or time-based IDs with configurable precision (seconds, milliseconds, microseconds, or nanoseconds).

It can generate a number of IDs and the ones generated in one go will be unique, but there's no generic uniquness guarantees.

It's best used for pre-generating a large number of IDs, for example to be used in spreadsheets such as Google Spreadsheets.

That said, usually you want to use other tools such as xid, snowflake, sonyflake or one of the hundreds of others

Features

  • Multiple ID Types: Pure random IDs or time-based IDs with different precisions
  • Sortable: Time-based IDs are lexicographically sortable
  • URL-Safe: Uses alphanumeric characters only, no special characters, and no confusing characters: 0, O, l, etc.
  • Collision-Free: Built-in duplicate detection ensures unique IDs in one go.
  • No Leading Numbers: Random suffixes never start with 0, to prevent unintentional removal in some tools.
  • CLI Tool: Easy-to-use command-line interface
  • Library: Use as a Go package in your applications

ID Format

Minid comes with a highly customizable format. By default it will generate a random 4-letter string. It can also prefix the random letters by a number of strings which represent time elapsed since 2025-01-01. For now this is hard coded, but could be easily modified if needed.

Furthermore you can pick the kind of time you want to use which could have a big impact on sortability.

Options are:

  • r: no sortabiliy, no timing.
  • s: seconds
  • m: milliseconds - This would allow you to generate dozens of IDs in a second and assume them to be more or less fully sortable.
  • u: microseconds - This would allow you to generate thousands of IDs in a second and assume them to be fully sortable
  • n: nanoseconds - With this option you can assume that all your IDs generated on a node to fully sortable as generating a minid takes much longer.

Note: (Partially) sortable IDs change the default for the number of random characters to optimize for shorter IDs.

Database storage

Minid IDs can be stored as byte slices or strings, up to you.

Installation

CLI Tool
go install github.com/peteraba/minid/cmd@latest

Or build from source:

make build

Usage

Command-Line Interface

Generate a single random ID (default):

minid

Generate multiple random IDs:

minid 10

Generate time-based IDs with different precisions:

minid s      # Unix seconds (default randLength: 3)
minid ms     # Unix milliseconds
minid us     # Unix microseconds
minid ns     # Unix nanoseconds

Generate multiple time-based IDs:

minid s 5    # 5 IDs with Unix seconds precision
minid ms 10  # 10 IDs with Unix milliseconds precision

Customize random suffix length:

minid -randLength 5    # or -rl 5
minid s -rl 6          # Unix seconds with 6-character random suffix

Examples:

$ minid
aB3x

$ minid 3
xY9z
mK2p
nL8q

$ minid s
132f3bXSZ

$ minid ms 5
11YFAUybhw7
11YFAUybzgu
11YFAUybEWK
11YFAUybF58
11YFAUybRd3

Development

Running Tests
make test
# or
go test ./...
Building
make build

License

See LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Minid

type Minid string

Minid is a single ID.

func FromBytes

func FromBytes(b []byte) (Minid, error)

FromBytes reconstructs a Minid from its byte representation.

func (Minid) Bytes

func (m Minid) Bytes() []byte

Bytes returns the ID as a byte slice.

func (Minid) String

func (m Minid) String() string

String returns the ID as a string.

type Minids

type Minids []Minid

Minids is a slice of Minid.

func Random

func Random(count, randLength int) Minids

Random generates random IDs.

func RandomNano

func RandomNano(count, randLength int) Minids

RandomNano generates random IDs with Unix nanosecond precision.

func RandomUnix

func RandomUnix(count, randLength int) Minids

RandomUnix generates random IDs with Unix second precision.

func RandomUnixMicro

func RandomUnixMicro(count, randLength int) Minids

RandomUnixMicro generates random IDs with Unix microsecond precision.

func RandomUnixMilli

func RandomUnixMilli(count, randLength int) Minids

RandomUnixMilli generates random IDs with Unix millisecond precision.

func (Minids) Print

func (m Minids) Print()

Print prints the IDs to the console.

func (Minids) Sort

func (m Minids) Sort()

Sort sorts the IDs lexicographically.

func (Minids) StringSlice

func (m Minids) StringSlice() []string

StringSlice returns a slice of strings representing the IDs.

type RandomType

type RandomType string
const (
	RandomTypeRandom    RandomType = "r"
	RandomTypeUnix      RandomType = "s"
	RandomTypeUnixMilli RandomType = "m"
	RandomTypeUnixMicro RandomType = "u"
	RandomTypeUnixNano  RandomType = "n"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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