randname

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 11 Imported by: 46 modules, 36 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

randname

Go Reference Build Status

A Go package which creates random names with an emphasis on flexibility and ease of use.

The default dictionaries are embedded at build time and do not need to be distributed separately. Go 1.16 or later is required.

Installation

From your Go project's directory, add the dependency:

go get github.com/marstr/randname@v0.2

If you're starting a new project, run go mod init example.com/myproject first, replacing the module path with your own.

Quick start

Save this as main.go and run it with go run .:

package main

import (
	"fmt"

	"github.com/marstr/randname"
)

func main() {
	fmt.Println(randname.Generate())
}

Generate() combines a random adjective, noun, and number from 00 to 99 in PascalCase, such as BigCloud09. No configuration is needed.

Choose a format

Use these helpers in place of Generate() to choose how the name is formatted:

fmt.Println(randname.GeneratePascalCase()) // e.g. BigCloud09
fmt.Println(randname.GenerateCamelCase())  // e.g. bigCloud09
fmt.Println(randname.GenerateHyphened())   // e.g. big-cloud-09

Each call generates a new random name; the comments illustrate the formats, not fixed output.

Configure a generator

The zero value of AdjNoun is ready to use. Nil Adjectives and Nouns use the embedded dictionaries, nil RandGenerator uses crypto/rand.Reader, and nil Format uses PascalCase. A non-nil empty dictionary omits that word.

var generator randname.AdjNoun
fmt.Println(generator.Generate()) // e.g. BigCloud09

To customize the generator, set fields in a struct literal:

generator := randname.AdjNoun{Format: randname.FormatHyphenedAdjNoun}
fmt.Println(generator.Generate())          // e.g. big-cloud-09
fmt.Println(generator.GenerateCamelCase()) // e.g. bigCloud09
fmt.Println(generator.Generate())          // still hyphened, e.g. small-river-42

The named formatting methods ignore Format for that call without changing it. A custom AdjNounFormat receives an adjective, noun, and integer from 0 to 99; it controls the output, including number padding.

Use a prefix

For a name with your own prefix and a random alphanumeric suffix:

fmt.Println(randname.GenerateWithPrefix("worker-", 6)) // e.g. worker-aB3xY7

The second argument is the suffix length; passing zero uses the default length of six.

See the Go reference for custom dictionaries, formatting functions, and other options.

Contributing

Feel free to send PRs my way! Just make sure that your code passes go test. If you add any publicly exported methods, please document them.

To contribute words, edit adjectives.txt or nouns.txt, keeping one word per line in alphabetical order. The files are embedded automatically on the next build; no Go code generation is needed.

Documentation

Index

Examples

Constants

View Source
const PrefixedDefaultLen = uint8(6)

PrefixedDefaultLen is the number of characters appended to a prefix when the length is zero.

Variables

View Source
var ArabicNumerals = []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}

ArabicNumerals contains the numeric digits 0-9.

View Source
var LowercaseAlphabet = []rune{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}

LowercaseAlphabet contains each letter of the alphaber, in lowercase.

View Source
var PrefixedDefaultAcceptable = append(append(append([]rune{}, ArabicNumerals...), UppercaseAlphabet...), LowercaseAlphabet...)

PrefixedDefaultAcceptable contains the characters that will be tacked onto the end of the Prefix, if none were provided.

View Source
var SpecialCharacters = []rune{'^', '$', '#', '*', '(', ')', '%', '@'}

SpecialCharacters holds a host of standard special characters.

View Source
var UppercaseAlphabet = []rune{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}

UppercaseAlphabet contains each letter of the alphabet, in uppercase.

Functions

func FormatCamelCaseAdjNoun added in v0.2.0

func FormatCamelCaseAdjNoun(adjective, noun string, digit int) string

FormatCamelCaseAdjNoun formats an adjective, noun, and number like bigCloud09. Nonnegative numbers are zero-padded to at least two decimal digits.

func FormatHyphenedAdjNoun added in v0.2.0

func FormatHyphenedAdjNoun(adjective, noun string, digit int) string

FormatHyphenedAdjNoun formats an adjective, noun, and number like big-cloud-09. Nonnegative numbers are zero-padded to at least two decimal digits.

func FormatPascalCaseAdjNoun added in v0.2.0

func FormatPascalCaseAdjNoun(adjective, noun string, digit int) string

FormatPascalCaseAdjNoun formats an adjective, noun, and number like BigCloud09. Nonnegative numbers are zero-padded to at least two decimal digits.

func Generate

func Generate() string

Generate returns a random name using the embedded dictionaries and PascalCase formatting, like BigCloud09. The number ranges from 00 to 99, inclusive.

Example
package main

import (
	"fmt"

	"github.com/marstr/randname"
)

func main() {
	fmt.Println(randname.Generate())
}

func GenerateCamelCase added in v0.2.0

func GenerateCamelCase() string

GenerateCamelCase returns a random name using the embedded dictionaries, formatted like bigCloud09, with a number from 00 to 99, inclusive.

Example
package main

import (
	"fmt"

	"github.com/marstr/randname"
)

func main() {
	fmt.Println(randname.GenerateCamelCase())
}

func GenerateHyphened added in v0.2.0

func GenerateHyphened() string

GenerateHyphened returns a random name using the embedded dictionaries, formatted like big-cloud-09, with a number from 00 to 99, inclusive.

Example
package main

import (
	"fmt"

	"github.com/marstr/randname"
)

func main() {
	fmt.Println(randname.GenerateHyphened())
}

func GeneratePascalCase added in v0.2.0

func GeneratePascalCase() string

GeneratePascalCase returns a random name using the embedded dictionaries, formatted like BigCloud09, with a number from 00 to 99, inclusive.

Example
package main

import (
	"fmt"

	"github.com/marstr/randname"
)

func main() {
	fmt.Println(randname.GeneratePascalCase())
}

func GenerateWithPrefix

func GenerateWithPrefix(prefix string, characters uint8) string

GenerateWithPrefix generates a string with a given prefix then a given number of randomly selected characters. If characters is zero, PrefixedDefaultLen (six) characters are appended.

Example
package main

import (
	"fmt"

	"github.com/marstr/randname"
)

func main() {
	for i := 0; i < 10; i++ {
		fmt.Println(randname.GenerateWithPrefix("randname-", 10))
	}
}

Types

type AdjNoun

type AdjNoun struct {
	// Adjectives supplies the adjective dictionary. Nil uses the embedded defaults;
	// a non-nil empty dictionary omits the adjective.
	Adjectives *collection.Dictionary
	// Format controls Generate's output. Nil uses FormatPascalCaseAdjNoun.
	Format AdjNounFormat
	// Nouns supplies the noun dictionary. Nil uses the embedded defaults;
	// a non-nil empty dictionary omits the noun.
	Nouns *collection.Dictionary
	// RandGenerator supplies randomness. Nil uses crypto/rand.Reader.
	RandGenerator io.Reader
}

AdjNoun generates a random adjective, noun, and number from 0 to 99, inclusive. Its zero value is ready to use and generates PascalCase names like BigCloud09.

func NewAdjNoun deprecated

func NewAdjNoun() *AdjNoun

NewAdjNoun returns a generator using PascalCase formatting and crypto/rand.Reader. Its dictionary fields are nil, so generation uses the embedded defaults.

Deprecated: Use the zero value instead, such as var generator AdjNoun.

func (AdjNoun) Generate

func (adNoun AdjNoun) Generate() string

Generate returns a random name using the generator's dictionaries, randomness, and Format. A nil Format uses PascalCase, like BigCloud09. The number passed to Format ranges from 0 to 99, inclusive.

Example
package main

import (
	"fmt"

	"github.com/marstr/randname"
)

func main() {
	var generator randname.AdjNoun
	fmt.Println(generator.Generate())
}

func (AdjNoun) GenerateCamelCase added in v0.2.0

func (adNoun AdjNoun) GenerateCamelCase() string

GenerateCamelCase returns a random name using the generator's dictionaries and randomness, formatted like bigCloud09, with a number from 00 to 99, inclusive. It ignores Format without changing it.

Example
package main

import (
	"fmt"

	"github.com/marstr/randname"
)

func main() {
	var generator randname.AdjNoun
	fmt.Println(generator.GenerateCamelCase())
}

func (AdjNoun) GenerateHyphened added in v0.2.0

func (adNoun AdjNoun) GenerateHyphened() string

GenerateHyphened returns a random name using the generator's dictionaries and randomness, formatted like big-cloud-09, with a number from 00 to 99, inclusive. It ignores Format without changing it.

Example
package main

import (
	"fmt"

	"github.com/marstr/randname"
)

func main() {
	var generator randname.AdjNoun
	fmt.Println(generator.GenerateHyphened())
}

func (AdjNoun) GeneratePascalCase added in v0.2.0

func (adNoun AdjNoun) GeneratePascalCase() string

GeneratePascalCase returns a random name using the generator's dictionaries and randomness, formatted like BigCloud09, with a number from 00 to 99, inclusive. It ignores Format without changing it.

Example
package main

import (
	"fmt"

	"github.com/marstr/randname"
)

func main() {
	var generator randname.AdjNoun
	fmt.Println(generator.GeneratePascalCase())
}

type AdjNounFormat

type AdjNounFormat func(string, string, int) string

AdjNounFormat combines an adjective, noun, and number into a formatted name. AdjNoun passes a number from 0 to 99, inclusive.

type FileDictionaryBuilder deprecated

type FileDictionaryBuilder struct {
	Target string
}

FileDictionaryBuilder read words from a file.

Deprecated: Populate a collection.Dictionary directly instead.

func (FileDictionaryBuilder) Build

func (fdb FileDictionaryBuilder) Build(dict *collection.Dictionary) (err error)

Build walks a simple newline delimited file of words and populates a dictionary with them.

type Prefixed

type Prefixed struct {
	Prefix     string
	Acceptable []rune
	// Len is the number of random characters to append. Zero uses PrefixedDefaultLen (six), not zero characters.
	Len           uint8
	RandGenerator io.Reader
}

Prefixed offers a means of generating a random name by providing a prefix, followed by a number of random letters or numbers.

Example
package main

import (
	"fmt"

	"github.com/marstr/randname"
)

func main() {
	generator := randname.Prefixed{
		Prefix:     "randname-",
		Acceptable: randname.ArabicNumerals,
		Len:        10,
	}

	for i := 0; i < 10; i++ {
		fmt.Println(generator.Generate())
	}
}

func (Prefixed) Generate

func (p Prefixed) Generate() string

Generate creates a string starting with a prefix, and ending with an assortment of randomly chosen runes.

Example
package main

import (
	"fmt"

	"github.com/marstr/randname"
)

func main() {
	generator := randname.Prefixed{
		Prefix: "randnameRocks",
	}
	fmt.Println(generator.Generate())
}

Jump to

Keyboard shortcuts

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