randname

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 10 Imported by: 36

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.

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 that will be appended to a suffix if no length was specified.

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 digit in the following way: bigCloud9

func FormatHyphenedAdjNoun added in v0.2.0

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

FormatHyphenedAdjNoun formats an adjective, noun, and digit in the following way: big-cloud-9

func FormatPascalCaseAdjNoun added in v0.2.0

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

FormatPascalCaseAdjNoun formats an adjective, noun, and digit in the following way: BigCloud9

func Generate

func Generate() string

Generate fetches a random name of the default dictionary and format.

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 fetches a random name using the default dictionary, formatted like: bigCloud9

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 fetches a random name using the default dictionary, formatted like: big-cloud-9

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 fetches a random name using the default dictionary, formatted like: BigCloud9

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.

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    *collection.Dictionary
	Format        AdjNounFormat
	Nouns         *collection.Dictionary
	RandGenerator io.Reader
}

AdjNoun creates a random name of the form adjectiveNameDigit

func NewAdjNoun

func NewAdjNoun() *AdjNoun

NewAdjNoun creates a new instance of AdjNoun that is populated with all of the defaults.

func (AdjNoun) Generate

func (adNoun AdjNoun) Generate() string

Generate creates a new randomly generated name with the

Example
package main

import (
	"fmt"

	"github.com/marstr/randname"
)

func main() {
	generator := randname.NewAdjNoun()
	fmt.Println(generator.Generate())
}

func (AdjNoun) GenerateCamelCase added in v0.2.0

func (adNoun AdjNoun) GenerateCamelCase() string

GenerateCamelCase randomly generates an adjective, noun, and number combination using the provided dictionaries, then returns them as a string formatted like: bigCloud9

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 randomly generates an adjective, noun, and number combination using the provided dictionaries, then returns them as a string formatted like: big-cloud-9

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 randomly generates an adjective, noun, and number then returns them as a string formatted like: BigCloud9

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 is a type of function that aggregates an Adjective, Noun, and Digit into a single formatted string.

type FileDictionaryBuilder

type FileDictionaryBuilder struct {
	Target string
}

FileDictionaryBuilder read words from a file.

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           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