Documentation
¶
Index ¶
- Variables
- func HumanUid(formatted ...bool) string
- func MicroUid(formatted ...bool) string
- func NanoUid(formatted ...bool) string
- func SecUid(formatted ...bool) string
- func Short(id string) (string, error)
- func ShortenBase16(id string) (string, error)
- func ShortenBase32(id string) (string, error)
- func ShortenBase36(id string) (string, error)
- func ShortenBase58(id string) (string, error)
- func ShortenBase62(id string) (string, error)
- func ShortenBase64(id string) (string, error)
- func ShortenCrockford(id string) (string, error)
- func ShortenZBase32(id string) (string, error)
- func Timestamp() string
- func TimestampMicro() string
- func TimestampNano() string
- func Unshort(s string) (string, error)
- func UnshortenBase16(s string) (string, error)
- func UnshortenBase32(s string) (string, error)
- func UnshortenBase36(s string) (string, error)
- func UnshortenBase58(s string) (string, error)
- func UnshortenBase62(s string) (string, error)
- func UnshortenBase64(s string) (string, error)
- func UnshortenCrockford(s string) (string, error)
- func UnshortenZBase32(s string) (string, error)
- func Uuid(formatted ...bool) string
- func UuidV1(formatted ...bool) string
- func UuidV3(namespace string, data []byte, formatted ...bool) (string, error)
- func UuidV4(formatted ...bool) string
- func UuidV5(namespace string, data []byte, formatted ...bool) (string, error)
- func UuidV6(formatted ...bool) string
- func UuidV7(formatted ...bool) string
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidByte = errors.New("character not in alphabet") ErrInvalidInput = errors.New("uid: invalid numeric input") )
Functions ¶
func HumanUid ¶
HumanUid generates a 32-character time-prefixed unique ID.
Format (conceptual): YYYYMMDDHHMMSSMMMMMMM + random suffix, truncated to 32.
Example (unformatted): 20250831151133000012345678901234 (length: 32) Example (formatted): 20171119-0849-2665-991498485465 (length: 35)
Parameters: - formatted: when true, include hyphens in groups 8-4-4-16 (length becomes 35)
Returns: - A 32-character uppercase numeric string suitable for human-readable IDs
func MicroUid ¶
MicroUid generates a 20-character time-prefixed unique ID.
Format (conceptual): YYYYMMDDHHMMSSMMMMMMM + random suffix, truncated to 20.
Example (unformatted): 20250831151133000012 (length: 20) Example (formatted): 20171119-084926-659914 (length: 22)
Parameters: - formatted: when true, include hyphens in groups 8-6-6 (length becomes 22)
Returns: - A 20-character numeric string
func NanoUid ¶
NanoUid generates a 23-character time-prefixed unique ID.
Format (conceptual): YYYYMMDDHHMMSSMMMMMMM + random suffix, truncated to 23.
Example (unformatted): 20250831151133000012345 (length: 23) Example (formatted): 20171119-084926-659914-984 (length: 26)
Parameters: - formatted: when true, include hyphens in groups 8-6-6-3 (length becomes 26)
Returns: - A 23-character numeric string
func SecUid ¶
SecUid generates a 14-character time-based ID.
Format: YYYYMMDDHHMMSS
Example (unformatted): 20250831151133 (length: 14) Example (formatted): 20171119-084926 (length: 15)
Parameters: - formatted: when true, include hyphens in groups 8-6 (length becomes 15)
Returns: - A 14-character numeric string representing UTC date/time to the second
func Short ¶ added in v1.9.0
Short converts a numeric ID string to a short, human-friendly representation using Base58.
Parameters: - id: A numeric string ID (may contain hyphens which are ignored).
Returns: - A shortened string representation, or an error if input is invalid.
func ShortenBase16 ¶ added in v1.9.0
ShortenBase16 converts a numeric string ID to a base16 (hexadecimal) string.
Benefits: - Standard hexadecimal representation. - Compatible with most systems and easily parsed.
Drawbacks: - Least compact representation; results in the longest strings.
Example: ShortenBase16("1234567890") -> "499602d2"
Parameters: - id: A numeric string represention of a positive integer.
Returns: - A lowercase hexadecimal string.
func ShortenBase32 ¶ added in v1.9.0
ShortenBase32 converts a numeric string ID to a base32 string (RFC 4648).
Benefits: - Case-insensitive and safe for filesystems. - Compact representation using only 32 characters.
Drawbacks: - Less compact than Base58/Base62. - Includes 'l' and 'i' which can be visually similar in some fonts.
Example: ShortenBase32("1234567890") -> "gnm6fsq"
Parameters: - id: A numeric string represention of a positive integer.
Returns: - A lowercase base32 string.
func ShortenBase36 ¶ added in v1.9.0
ShortenBase36 converts a numeric string ID to a base36 string.
Benefits: - Uses all digits and lowercase letters (0-9, a-z). - Case-insensitive and widely used for compact IDs.
Drawbacks: - Includes 'l', '1', 'o', and '0', which can be visually confusing.
Example: ShortenBase36("1234567890") -> "kf12oi"
Parameters: - id: A numeric string represention of a positive integer.
Returns: - A lowercase base36 string.
func ShortenBase58 ¶ added in v1.9.0
ShortenBase58 converts a numeric string ID to a base58 string.
Benefits: - Human-friendly: avoids ambiguous characters like 0, O, I, and l. - No non-alphanumeric characters, making it safe for double-clicking to select.
Drawbacks: - Case-sensitive; requires exact casing for decoding.
Example: ShortenBase58("1234567890") -> "2V6G2p"
Parameters: - id: A numeric string represention of a positive integer.
Returns: - A base58 string (mixed case).
func ShortenBase62 ¶ added in v1.9.0
ShortenBase62 converts a numeric string ID to a base62 string.
Benefits: - Maximum density using all standard alphanumeric characters (0-9, A-Z, a-z). - Very compact and widely supported by URL shorteners.
Drawbacks: - Case-sensitive, prone to manual entry errors. - Includes visually similar characters (e.g., 'I', 'l', '1', 'O', '0').
Example: ShortenBase62("1234567890") -> "1LY7vk"
Parameters: - id: A numeric string represention of a positive integer.
Returns: - A base62 string (mixed case).
func ShortenBase64 ¶ added in v1.9.0
ShortenBase64 converts a numeric string ID to a URL-safe base64 string.
Benefits: - Extremely compact representation. - Safe for use in URLs (uses '-' and '_' instead of '+' and '/').
Drawbacks: - Case-sensitive, which can lead to errors if manually typed. - Contains visually similar characters (e.g., 'O' and '0', 'I' and 'l'). - Includes '-' and '_', which can interfere with double-click selection in some UIs. - Not recommended for human-facing IDs; consider Base58 or Crockford instead.
Example: ShortenBase64("1234567890") -> "BJlg8u"
Parameters: - id: A numeric string represention of a positive integer.
Returns: - A URL-safe base64 string.
func ShortenCrockford ¶ added in v1.9.0
ShortenCrockford converts a numeric string ID to a Crockford Base32 string.
Benefits: - Highly human-readable and designed to avoid common transcription errors. - Excludes ambiguous characters like I, L, O, and U.
Drawbacks: - Less compact than Base62/Base64.
Example: ShortenCrockford("1234567890") -> "14MM0M"
Parameters: - id: A numeric string represention of a positive integer.
Returns: - An uppercase Crockford Base32 string.
func ShortenZBase32 ¶ added in v1.9.0
ShortenZBase32 converts a numeric string ID to a z-base-32 string.
Benefits: - Optimized for human readability and memorability. - Alphabet is designed to put easiest-to-recognize characters in most-used positions.
Drawbacks: - Non-standard alphabet; not as widely supported as RFC 4648 Base32.
Example: ShortenZBase32("1234567890") -> "4pk9xyo"
Parameters: - id: A numeric string represention of a positive integer.
Returns: - A lowercase z-base-32 string.
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 Unshort ¶ added in v1.9.0
Unshort converts a shortened string (Base58) back to its original numeric ID.
Parameters: - s: A shortened string ID (Base58).
Returns: - The original numeric string ID, or an error if decoding fails.
func UnshortenBase16 ¶ added in v1.9.0
UnshortenBase16 converts a base16 (hexadecimal) string back to its original numeric string ID.
Example: UnshortenBase16("499602d2") -> "1234567890"
Parameters: - s: A hexadecimal string.
Returns: - The original numeric string ID, or an error if invalid characters are encountered.
func UnshortenBase32 ¶ added in v1.9.0
UnshortenBase32 converts a base32 string back to its original numeric string ID.
Example: UnshortenBase32("gnm6fsq") -> "1234567890"
Parameters: - s: A base32 string (case-insensitive).
Returns: - The original numeric string ID, or an error if invalid characters are encountered.
func UnshortenBase36 ¶ added in v1.9.0
UnshortenBase36 converts a base36 string back to its original numeric string ID.
Example: UnshortenBase36("kf12oi") -> "1234567890"
Parameters: - s: A base36 string (case-insensitive).
Returns: - The original numeric string ID, or an error if invalid characters are encountered.
func UnshortenBase58 ¶ added in v1.9.0
UnshortenBase58 converts a base58 string back to its original numeric string ID.
Example: UnshortenBase58("2V6G2p") -> "1234567890"
Parameters: - s: A base58 string.
Returns: - The original numeric string ID, or an error if invalid characters are encountered.
func UnshortenBase62 ¶ added in v1.9.0
UnshortenBase62 converts a base62 string back to its original numeric string ID.
Example: UnshortenBase62("1LY7vk") -> "1234567890"
Parameters: - s: A base62 string.
Returns: - The original numeric string ID, or an error if invalid characters are encountered.
func UnshortenBase64 ¶ added in v1.9.0
UnshortenBase64 converts a URL-safe base64 string back to its original numeric string ID.
Example: UnshortenBase64("BJlg8u") -> "1234567890"
Parameters: - s: A URL-safe base64 string.
Returns: - The original numeric string ID, or an error if invalid characters are encountered.
func UnshortenCrockford ¶ added in v1.9.0
UnshortenCrockford converts a Crockford Base32 string back to its original numeric string ID.
Features: - Supports normalization: converts to uppercase, removes hyphens. - Maps visually similar characters (O -> 0, I/L -> 1) to reduce errors.
Example: UnshortenCrockford("14MM0M") -> "1234567890"
Parameters: - s: A Crockford Base32 string.
Returns: - The original numeric string ID, or an error if invalid characters are encountered.
func UnshortenZBase32 ¶ added in v1.9.0
UnshortenZBase32 converts a z-base-32 string back to its original numeric string ID.
Example: UnshortenZBase32("4pk9xyo") -> "1234567890"
Parameters: - s: A z-base-32 string (case-insensitive).
Returns: - The original numeric string ID, or an error if invalid characters are encountered.
func Uuid ¶
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: - formatted: when true, include hyphens
Returns: - A random UUID (version 4) without hyphens
func UuidV1 ¶
UuidV1 returns a version 1 (time-based) UUID without hyphens.
Example: 6ba7b8109dad11d180b400c04fd430c8 (length: 32)
Parameters: - formatted: when true, include hyphens
Returns: - The UUID v1 as a string
func UuidV3 ¶
UuidV3 returns a version 3 (MD5 name-based) UUID. 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 - formatted: when true, include hyphens
Returns: - The UUID v3 as a string, or an error
func UuidV4 ¶
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: - formatted: when true, include hyphens
Returns: - A random UUID (version 4) without hyphens
func UuidV5 ¶
UuidV5 returns a version 5 (SHA-1 name-based) UUID. 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 - formatted: when true, include hyphens
Returns: - The UUID v5 as a string, or an error
func UuidV6 ¶
UuidV6 returns a version 6 (time-ordered) UUID without hyphens.
Example: 1ed0c9e48f7b6b2c9c3b6a6c7a9d5e12 (length: 32)
Parameters: - formatted: when true, include hyphens
Returns: - A UUID v6 (time-ordered) without hyphens
func UuidV7 ¶
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: - formatted: when true, include hyphens
Returns: - A UUID v7 (Unix time-based) without hyphens
Types ¶
This section is empty.