Documentation
¶
Overview ¶
Package petname is a library for generating human-readable, random names for objects (e.g. hostnames, containers, blobs).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Adjective ¶
func Adjective() string
Adjective returns a random adjective from a list of petname adjectives. It uses the global random source (automatically seeded in Go 1.20+).
func Adverb ¶
func Adverb() string
Adverb returns a random adverb from a list of petname adverbs. It uses the global random source (automatically seeded in Go 1.20+).
func Generate ¶
Generate generates and returns a random pet name. It takes two parameters: the number of words in the name, and a separator token. If a single word is requested, simply a Name() is returned. If two words are requested, an Adjective() and a Name() are returned. If three or more words are requested, a variable number of Adverb()s, an Adjective(), and a Name() are returned. The separator can be any character, string, or the empty string. It uses the global random source (automatically seeded in Go 1.20+).
For deterministic or concurrent use, create a Generator with New() instead.
Example ¶
Example of using the traditional package-level functions (backward compatible)
// Old API - still works exactly as before // Uses global rand (auto-seeded in Go 1.20+) name := petname.Generate(2, "-") fmt.Println(name)
func Name ¶
func Name() string
Name returns a random name from a list of petname names. It uses the global random source (automatically seeded in Go 1.20+).
func NonDeterministicMode
deprecated
func NonDeterministicMode()
NonDeterministicMode seeds the random number generator with the current time.
Deprecated: As of Go 1.20, the global random generator is automatically seeded at program startup, making this function unnecessary. It is maintained for backward compatibility but has no effect when using Go 1.20+. See: https://go.dev/doc/go1.20#math/rand
Types ¶
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator generates random petnames using a specific random source. This allows for deterministic generation (e.g., for testing) and thread-safe concurrent use with separate random sources.
func New ¶
New creates a new Generator with the specified random source. If rnd is nil, the global math/rand functions are used (same as package-level functions). For deterministic or concurrent use, pass rand.New(rand.NewSource(seed)).
Example (Concurrent) ¶
Example of using the new Generator API for concurrent use
// Each goroutine can have its own generator for thread-safe concurrent use gen1 := petname.New(rand.New(rand.NewSource(1))) gen2 := petname.New(rand.New(rand.NewSource(2))) // These can be safely used concurrently name1 := gen1.Generate(2, "-") name2 := gen2.Generate(2, "-") fmt.Println(name1) fmt.Println(name2)
Output: touched-shark relaxing-muskox
Example (Deterministic) ¶
Example of using the new Generator API with deterministic seed
// New API - deterministic generation for testing gen := petname.New(rand.New(rand.NewSource(42))) // Generate predictable names fmt.Println(gen.Generate(2, "-")) fmt.Println(gen.Generate(2, "-")) fmt.Println(gen.Generate(2, "-"))
Output: guiding-dodo relieved-bass mature-zebra
func (*Generator) Adjective ¶
Adjective returns a random adjective from the generator's random source.
Example ¶
Example of generating individual words with deterministic behavior
gen := petname.New(rand.New(rand.NewSource(100))) fmt.Println(gen.Adjective()) fmt.Println(gen.Adverb()) fmt.Println(gen.Name())
Output: skilled apparently prawn
func (*Generator) Generate ¶
Generate generates a random pet name with the specified number of words and separator. If a single word is requested, simply a Name() is returned. If two words are requested, an Adjective() and a Name() are returned. If three or more words are requested, a variable number of Adverb()s, an Adjective(), and a Name() are returned. The separator can be any character, string, or the empty string.