uid

package module
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2025 License: MIT Imports: 5 Imported by: 28

README

UID (Unique ID) Open in Gitpod

Tests Status Go Report Card PkgGoDev

This package generates unique identifying strings. Largest attention is paid on human friendly unique identifiers (dated digits).

Installation

go get -u github.com/dracory/uid

Usage

package main

import (
    "fmt"
    "github.com/dracory/uid"
)

func main() {
    // HumanUid generates a UID (32 digits)
    // Format: YYYYMMDD-HHMM-SSMM-MMMMNNNRRRRRRRRR
    human := uid.HumanUid()      // length: 32

    // NanoUid generates a UID (23 digits)
    // Format: YYYYMMDD-HHMMSS-MMMMMM-NNN
    nano := uid.NanoUid()        // length: 23

    // MicroUid generates a UID (20 digits)
    // Format: YYYYMMDD-HHMMSS-MMMMMM
    micro := uid.MicroUid()      // length: 20

    // SecUid generates a UID (14 digits)
    // Format: YYYYMMDD-HHMMSS
    sec := uid.SecUid()          // length: 14

    // Unix timestamps as strings
    ts := uid.Timestamp()        // seconds, length: 10
    tsu := uid.TimestampMicro()  // microseconds, length: 16
    tsn := uid.TimestampNano()   // nanoseconds, length: 19

    // UUIDs (via github.com/google/uuid)
    u := uid.Uuid()              // v4 without hyphens, length: 32
    uf := uid.UuidFormatted()    // v4 with hyphens, length: 36

    fmt.Println(human, nano, micro, sec, ts, tsu, tsn, u, uf)
}

Supported UID Types

It supports several types of unique identifiers.

The type you want to use will usually depends on two considerations:

  1. How random you want it to be? The longer the identifier, the more the chances of collision reduce
  2. How long you want the identifier to be? The longer the identifier, reduces the readability, as well as the storage space to store it.

For most of the user cases a Micro UUID (20 chars) should be fine. A human UUID (32 chars) should be avoided where a human is involved as too "mind bogging" to work with.

  1. Human UUID (32 digits)

    Format: YYYYMMDD-HHMM-SSMM-MMMMNNNRRRRRRRRR

    2017111908492665991498485465 (with dashes: 20171119-0849-2665-991498485465)

  2. Nano UID (23 digits)

    Format: YYYYMMDD-HHMMSS-MMMMMM-NNN

    Examples:

    20171119084926659914984 (with dashes: 20171119-084926-659914-984)

  3. Micro UID (20 digits)

    Format: YYYYMMDD-HHMMSS-MMMMMM

    Examples:

    20171119084926659914 (with dashes: 20171119-084926-659914)

  4. Seconds UID (14 digits)

    Format: YYYYMMDD-HHMMSS

    Examples:

    20171119084926 (with dashes: 20171119-084926)

  5. Timestamp (10 digits) Unit timestamp, seconds precision

    Format: 1234567890

    Examples:

    1704524414

  6. TimestampMicro (16 digits) Unit timestamp, microseconds precision

    Format: 1234567890123456

    Examples:

    1704524414548721

  7. TimestampNano (19 digits) Unit timestamp, nanoseconds precision

    Format: 1234567890123456789

    Examples:

    1704524414548721308

  8. Uuid (32 characters) Random V4 UUID. UUID (Universally Unique IDentifier), also known as GUID (Globally Unique IDentifier)

    Format: abcdef1234567890abcdef1234567890

    Examples:

    459e2999bd071151a23d643da42c2cc2

UUID functions

All UUID functions are thin wrappers around github.com/google/uuid.

  • Uuid() → v4 without hyphens Example: 550e8400e29b41d4a716446655440000 (length: 32)

  • UuidFormatted() → v4 with hyphens Example: 550e8400-e29b-41d4-a716-446655440000 (length: 36)

  • UuidV1() / UuidV1Formatted() → version 1 (time-based) Examples: 6ba7b8109dad11d180b400c04fd430c8 (32) • 6ba7b810-9dad-11d1-80b4-00c04fd430c8 (36)

  • UuidV3(namespace, data) / UuidV3Formatted(namespace, data) → version 3 (MD5 name-based) Examples: 3d813cbb47fb32ba91df831e1593ac29 (32) • 3d813cbb-47fb-32ba-91df-831e1593ac29 (36)

  • UuidV4() / UuidV4Formatted() → version 4 (random) Examples: 550e8400e29b41d4a716446655440000 (32) • 550e8400-e29b-41d4-a716-446655440000 (36)

  • UuidV5(namespace, data) / UuidV5Formatted(namespace, data) → version 5 (SHA-1 name-based) Examples: 21f7f8de80515b8986800195ef798b6a (32) • 21f7f8de-8051-5b89-8680-0195ef798b6a (36)

  • UuidV6() / UuidV6Formatted() → version 6 (time-ordered) Examples: 1ed0c9e48f7b6b2c9c3b6a6c7a9d5e12 (32) • 1ed0c9e4-8f7b-6b2c-9c3b-6a6c7a9d5e12 (36)

  • UuidV7() / UuidV7Formatted() → version 7 (Unix time-based) Examples: 01890f5f3d9c7a0e8a7b6c5d4e3f2a10 (32) • 01890f5f-3d9c-7a0e-8a7b-6c5d4e3f2a10 (36)

Change Log

2024.01.06 - Added Timestamp and Uuid functions 2021.12.19 - Master branch changed to main 2021.12.19 - Added tests

Similar Packages

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HumanUid

func HumanUid() string

HumanUid generates a 32-character time-prefixed unique ID.

Format (conceptual): YYYYMMDDHHMMSSMMMMMMM + random suffix, truncated to 32.

Example: 20250831151133000012345678901234 (length: 32)

Parameters: - None

Returns: - A 32-character uppercase numeric string suitable for human-readable IDs

func MicroUid

func MicroUid() string

MicroUid generates a 20-character time-prefixed unique ID.

Format (conceptual): YYYYMMDDHHMMSSMMMMMMM + random suffix, truncated to 20.

Example: 20250831151133000012 (length: 20)

Parameters: - None

Returns: - A 20-character numeric string

func NanoUid

func NanoUid() string

NanoUid generates a 23-character time-prefixed unique ID.

Format (conceptual): YYYYMMDDHHMMSSMMMMMMM + random suffix, truncated to 23.

Example: 20250831151133000012345 (length: 23)

Parameters: - None

Returns: - A 23-character numeric string

func SecUid

func SecUid() string

SecUid generates a 14-character time-based ID.

Format: YYYYMMDDHHMMSS

Example: 20250831151133 (length: 14)

Parameters: - None

Returns: - A 14-character numeric string representing UTC date/time to the second

func Timestamp

func Timestamp() string

Timestamp returns the current Unix timestamp in seconds as a string.

Example: 1725111153 (length: 10)

Parameters: - None

Returns: - Unix timestamp in seconds (base-10 string)

func TimestampMicro

func TimestampMicro() string

TimestampMicro returns the current Unix timestamp in microseconds as a string.

Example: 1725111153123456 (length: 16)

Parameters: - None

Returns: - Unix timestamp in microseconds (base-10 string)

func TimestampNano

func TimestampNano() string

TimestampNano returns the current Unix timestamp in nanoseconds as a string.

Example: 1725111153123456789 (length: 19)

Parameters: - None

Returns: - Unix timestamp in nanoseconds (base-10 string)

func Uuid

func Uuid() string

Uuid returns a random UUID (version 4) without hyphens.

Example: 550e8400e29b41d4a716446655440000 (length: 32)

https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)

Parameters: - None

Returns: - A random UUID (version 4) without hyphens

func UuidFormatted

func UuidFormatted() string

UuidFormatted returns a random UUID (version 4) string with hyphens.

Example: 550e8400-e29b-41d4-a716-446655440000 (length: 36)

https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)

Parameters: - None

Returns: - A random UUID (version 4) with hyphens

func UuidV1

func UuidV1() string

UuidV1 returns a version 1 (time-based) UUID without hyphens.

Example: 6ba7b8109dad11d180b400c04fd430c8 (length: 32)

https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_1_and_6_(date-time_and_MAC_address)

Parameters: - None

Returns: - A UUID v1 (time-based) without hyphens

func UuidV1Formatted

func UuidV1Formatted() string

UuidV1Formatted returns a version 1 (time-based) UUID with hyphens.

Example: 6ba7b810-9dad-11d1-80b4-00c04fd430c8 (length: 36)

https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_1_and_6_(date-time_and_MAC_address)

Parameters: - None

Returns: - A UUID v1 (time-based) with hyphens

func UuidV3

func UuidV3(namespace string, data []byte) (string, error)

UuidV3 returns a version 3 (MD5 name-based) UUID without hyphens. Provide a 16-byte namespace UUID and arbitrary data.

Example (no hyphens): 3d813cbb47fb32ba91df831e1593ac29 (length: 32)

https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based)

Parameters: - namespace: a 16-byte UUID (as bytes) used as the namespace - data: the name bytes to hash

Returns: - The UUID v3 as a 32-character string without hyphens, or an error

func UuidV3Formatted

func UuidV3Formatted(namespace string, data []byte) (string, error)

UuidV3Formatted returns a version 3 (MD5 name-based) UUID with hyphens. Provide a 16-byte namespace UUID and arbitrary data.

Example: 3d813cbb-47fb-32ba-91df-831e1593ac29 (length: 36)

https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based)

Parameters: - namespace: a 16-byte UUID (as bytes) used as the namespace - data: the name bytes to hash

Returns: - The UUID v3 as a 36-character string with hyphens, or an error

func UuidV4

func UuidV4() string

UuidV4 returns a random UUID (version 4) without hyphens.

Example: 550e8400e29b41d4a716446655440000 (length: 32)

https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)

Parameters: - None

Returns: - A random UUID (version 4) without hyphens

func UuidV4Formatted

func UuidV4Formatted() string

UuidV4Formatted returns a random UUID (version 4) with hyphens.

Example: 550e8400-e29b-41d4-a716-446655440000 (length: 36)

https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)

Parameters: - None

Returns: - A random UUID (version 4) with hyphens

func UuidV5

func UuidV5(namespace string, data []byte) (string, error)

UuidV5 returns a version 5 (SHA-1 name-based) UUID without hyphens. Provide a 16-byte namespace UUID and arbitrary data.

Example (no hyphens): 21f7f8de80515b8986800195ef798b6a (length: 32)

https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based)

Parameters: - namespace: a 16-byte UUID (as bytes) used as the namespace - data: the name bytes to hash

Returns: - The UUID v5 as a 32-character string without hyphens, or an error

func UuidV5Formatted

func UuidV5Formatted(namespace string, data []byte) (string, error)

UuidV5Formatted returns a version 5 (SHA-1 name-based) UUID with hyphens. Provide a 16-byte namespace UUID and arbitrary data.

Example: 21f7f8de-8051-5b89-8680-0195ef798b6a (length: 36)

https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based)

Parameters: - namespace: a 16-byte UUID (as bytes) used as the namespace - data: the name bytes to hash

Returns: - The UUID v5 as a 36-character string with hyphens, or an error

func UuidV6

func UuidV6() string

UuidV6 returns a version 6 (time-ordered) UUID without hyphens.

Example: 1ed0c9e48f7b6b2c9c3b6a6c7a9d5e12 (length: 32)

Draft: https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_1_and_6_(date-time_and_MAC_address)

Parameters: - None

Returns: - A UUID v6 (time-ordered) without hyphens

func UuidV6Formatted

func UuidV6Formatted() string

UuidV6Formatted returns a version 6 (time-ordered) UUID with hyphens.

Example: 1ed0c9e4-8f7b-6b2c-9c3b-6a6c7a9d5e12 (length: 36)

Draft:https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_1_and_6_(date-time_and_MAC_address)

Parameters: - None

Returns: - A UUID v6 (time-ordered) with hyphens

func UuidV7

func UuidV7() string

UuidV7 returns a version 7 (Unix time-based) UUID without hyphens.

Example: 01890f5f3d9c7a0e8a7b6c5d4e3f2a10 (length: 32)

Draft: https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_7_(timestamp_and_random)

Parameters: - None

Returns: - A UUID v7 (Unix time-based) without hyphens

func UuidV7Formatted

func UuidV7Formatted() string

UuidV7Formatted returns a version 7 (Unix time-based) UUID with hyphens.

Example: 01890f5f-3d9c-7a0e-8a7b-6c5d4e3f2a10 (length: 36)

Draft: https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_7_(timestamp_and_random)

Parameters: - None

Returns: - A UUID v7 (Unix time-based) with hyphens

Types

This section is empty.

Jump to

Keyboard shortcuts

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