uid

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package uid generates prefixed random identifiers for Wappiz resources.

The package provides two main functions for generating random strings: New for prefixed identifiers and [DNS1035] for DNS-compliant labels.

Security

This package uses math/rand/v2 which is NOT cryptographically secure. The generated identifiers are predictable and MUST NOT be used for API keys, session tokens, or any security-sensitive purposes. Use crypto/rand directly for those cases.

Usage

Generate a prefixed identifier:

id := uid.New(uid.RequestPrefix)      // "req_k3n5p8x2"

Generate a DNS-1035 compliant label:

label := uid.DNS1035()    // "k3n5p8x2" (starts with letter)
label := uid.DNS1035(12)  // "a9k2n5p8x3m7"

Prefixes

Standard prefixes are defined as Prefix constants (RequestPrefix, APIPrefix) to make IDs self-descriptive. See prefix.go for the complete list.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(prefix Prefix, length ...int) string

New generates a prefixed random identifier.

The identifier consists of the prefix, an underscore separator, and random alphanumeric characters. Default random portion is 8 characters; pass a custom length to override.

Pass an empty prefix to generate an identifier without a prefix.

Uses math/rand/v2 which is NOT cryptographically secure. Do not use for API keys, tokens, or security-sensitive purposes.

func Secure

func Secure(length ...int) string

Secure generates a cryptographically secure random identifier.

Uses crypto/rand for secure random generation. Use this for verification tokens, API keys, or any security-sensitive purposes.

The identifier consists of random alphanumeric characters. Default length is 24 characters; pass a custom length to override.

Types

type Prefix

type Prefix string

Prefix is a resource type identifier prepended to generated IDs.

const (
	RequestPrefix Prefix = "req"
	TestPrefix    Prefix = "test" // for tests only
)

Jump to

Keyboard shortcuts

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