utils

package
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Overview

Package utils provides utility functions for common operations including UUID generation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultString added in v0.24.0

func DefaultString(value, defaultValue string) string

DefaultString returns the value if it's not empty, otherwise returns the default value

func ExpandPath

func ExpandPath(path string) (string, error)

func ExpandPathPOSIX added in v0.19.0

func ExpandPathPOSIX(path string) (string, error)
Example
p, err := ExpandPathPOSIX("~/test")
if err != nil {
	log.Fatal(err)
}

_, last := filepath.Split(p)
fmt.Println(last)

p, _ = ExpandPathPOSIX("~NONEXISTINGUSER/path/to/file")
fmt.Println(p)

p, _ = ExpandPathPOSIX("/path/to/file/tilde/~")
fmt.Println(p)

p, _ = ExpandPathPOSIX("")
fmt.Println(p)

p, _ = ExpandPathPOSIX("")
fmt.Println(p)
Output:
test
~NONEXISTINGUSER/path/to/file
/path/to/file/tilde/~

func GenerateUUID added in v0.24.0

func GenerateUUID() string

GenerateUUID generates a random UUID v4 string. Returns a 36-character UUID in format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".

Example:

id := utils.GenerateUUID()
// Output: "550e8400-e29b-41d4-a716-446655440000"

func GenerateUUIDShort added in v0.24.0

func GenerateUUIDShort() string

GenerateUUIDShort generates a short 8-character UUID string. Higher collision probability than full UUID - use carefully in production.

Example:

id := utils.GenerateUUIDShort()
// Output: "550e8400"

func GenerateUUIDv5 added in v0.24.0

func GenerateUUIDv5(namespace uuid.UUID, fields []string) (string, error)

GenerateUUIDv5 generates a deterministic UUID v5 from namespace and fields. Same inputs always produce the same UUID. Fields are joined with '|' separator.

Parameters:

  • namespace: UUID namespace (e.g., uuid.NameSpaceDNS)
  • fields: Non-empty strings to generate UUID from

Returns error if fields is empty or contains empty strings.

Example:

id, err := utils.GenerateUUIDv5(uuid.NameSpaceDNS, []string{"user", "john.doe"})

Types

This section is empty.

Jump to

Keyboard shortcuts

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