Documentation
¶
Overview ¶
Package random provides utility functions for generating various types of random data.
Index ¶
- func Bool() bool
- func Digit() string
- func Digits(length int) string
- func Edit(input string) (out string)
- func Element[T any](slice []T) T
- func Email() string
- func ImageAbstractPNG(whOpt ...int) ([]byte, error)
- func ImagePNG(whOpt ...int) ([]byte, error)
- func Int(from, to int) int
- func Letter() string
- func Letters(length int) string
- func Maybe[T any](val T, probabilityOpt ...int) T
- func Patch(input string) (patch string)
- func ReleaseDate() string
- func String(lengthOpt ...int) string
- func Title() string
- func URL() string
- func ValOrNil[T any](val T, probabilityOpt ...int) *T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Edit ¶
Edit applies a random modification to the input string that is nicely fit to diff comparison and patch creation. The function randomly performs one of six operations:
- For multi-word strings (split by spaces): adds/removes entire words
- For single-word strings: adds/removes individual characters
If input is empty, it generates a random title first.
func Element ¶
func Element[T any](slice []T) T
Element returns a random element from the provided slice. If the slice is empty, it returns the zero value for type T.
func ImageAbstractPNG ¶
ImageAbstractPNG generates a random abstract image with gradients and shapes.
func ImagePNG ¶
ImagePNG generates an in-memory PNG image and returns its encoded bytes. By default, it creates a square image with random size between 100 and 500 pixels. Optional arguments allow overriding the dimensions:
- no arguments: random square image (w == h)
- one argument: square image with given width/height
- two arguments: image with explicit width and height
The image content is a simple deterministic color gradient based on pixel coordinates, useful for tests, placeholders, or fixtures.
func Int ¶
Int generates a random integer within the specified inclusive range [min, max]. If max is less than min, the arguments are swapped to ensure the range is valid.
func Letter ¶
func Letter() string
Letter returns a single random alphabetic character (a-z, A-Z) as a string.
func Letters ¶
Letters generates a random string consisting only of alphabetic characters (a-z, A-Z).
func Maybe ¶
Maybe returns either the provided value or the zero value of T, based on the given probability.
func ReleaseDate ¶
func ReleaseDate() string
ReleaseDate returns a randomly generated release date formatted using one of the allowed release date layouts.
func String ¶
String generates a random alphanumeric string. The length of the string defaults to 10 if no argument is provided.
func ValOrNil ¶
ValOrNil returns a pointer to val or nil based on the given probability. The probability is specified in percent (0–100) and defaults to 50% if omitted.
Examples:
ValOrNil("hello") // ~50% chance to return &"hello"
ValOrNil("hello", 10) // 10% chance to return &"hello"
ValOrNil("hello", 100) // always returns &"hello"
ValOrNil("hello", 0) // always returns nil
Types ¶
This section is empty.