faker

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package faker provides deterministic fake data generators for use with seedling's Generate option. All methods derive output solely from the wrapped *rand.Rand, ensuring reproducible test data when the same seed is used.

Basic usage with seedling.Generate:

seedling.Generate(func(r *rand.Rand, u *User) {
    f := faker.New(r)
    u.Name = f.Name()
    u.Email = f.Email()
})

Faker is NOT safe for concurrent use.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Pick

func Pick[T any](f *Faker, items []T) T

Pick returns a random element from items. It panics if items is empty.

Example
f := faker.New(rand.New(rand.NewPCG(1, 1)))
colors := []string{"red", "green", "blue"}
fmt.Println(faker.Pick(f, colors))
Output:
blue

Types

type Faker

type Faker struct {
	// contains filtered or unexported fields
}

Faker generates deterministic fake data from a *rand.Rand source.

func Default

func Default() *Faker

Default creates a Faker with a non-deterministic seed. Use New with a fixed seed for reproducible tests.

func New

func New(r *rand.Rand) *Faker

New creates a Faker backed by the given RNG with the default "en" locale. Passing the *rand.Rand from seedling.Generate ensures deterministic output. It panics if r is nil.

Example
f := faker.New(rand.New(rand.NewPCG(42, 42)))

fmt.Println(f.Name())
fmt.Println(f.Email())
fmt.Println(f.Phone())
fmt.Println(f.Address())
fmt.Println(f.UUID())
Output:
Cynthia Torres
kathleen.roberts@local.test
+1-339-323-7574
8197 North St
5171f6ef-7782-4870-83f6-83d1b4fa890c

func NewWithLocale added in v0.2.0

func NewWithLocale(r *rand.Rand, locale string) *Faker

NewWithLocale creates a Faker backed by the given RNG with the specified locale. Supported locales: "en", "ja", "zh", "ko", "de", "fr". It panics if r is nil or the locale is unknown.

func (*Faker) Address

func (f *Faker) Address() string

Address returns a random street address in a locale-appropriate format.

func (*Faker) Bool

func (f *Faker) Bool() bool

Bool returns a random boolean.

func (*Faker) City

func (f *Faker) City() string

City returns a random city name.

Example
f := faker.New(rand.New(rand.NewPCG(1, 1)))
fmt.Println(f.City())
Output:
Detroit

func (*Faker) Country

func (f *Faker) Country() string

Country returns a random country name.

func (*Faker) CreditCard

func (f *Faker) CreditCard() string

CreditCard returns a random credit card number in XXXX-XXXX-XXXX-XXXX format.

func (*Faker) Date

func (f *Faker) Date() time.Time

Date returns a random date between 2000-01-01 and 2030-12-31.

func (*Faker) DateBetween

func (f *Faker) DateBetween(from, to time.Time) time.Time

DateBetween returns a random time between from and to (inclusive). It panics if from is after to.

func (*Faker) Email

func (f *Faker) Email() string

Email returns a random email address. For non-Latin locales, romanized names are used to produce valid ASCII addresses.

func (*Faker) FirstName

func (f *Faker) FirstName() string

FirstName returns a random first name.

func (*Faker) Float

func (f *Faker) Float() float64

Float returns a random float64 in [0.0, 1.0).

func (*Faker) FloatBetween

func (f *Faker) FloatBetween(min, max float64) float64

FloatBetween returns a random float64 in [min, max).

func (*Faker) HexColor

func (f *Faker) HexColor() string

HexColor returns a random hex color string like "#a3c1f0".

func (*Faker) IPv4

func (f *Faker) IPv4() string

IPv4 returns a random IPv4 address string.

func (*Faker) Int

func (f *Faker) Int() int

Int returns a random non-negative int.

func (*Faker) IntBetween

func (f *Faker) IntBetween(min, max int) int

IntBetween returns a random int in [min, max]. It panics if min > max.

func (*Faker) LastName

func (f *Faker) LastName() string

LastName returns a random last name.

func (*Faker) Name

func (f *Faker) Name() string

Name returns a random full name. For CJK locales (ja, zh, ko), the name is formatted as LastFirst (no space).

func (*Faker) Paragraph

func (f *Faker) Paragraph() string

Paragraph returns a random paragraph of 3-6 sentences.

func (*Faker) Phone

func (f *Faker) Phone() string

Phone returns a random phone number in a locale-appropriate format.

func (*Faker) Sentence

func (f *Faker) Sentence() string

Sentence returns a random sentence of 5-12 words.

func (*Faker) URL

func (f *Faker) URL() string

URL returns a random URL.

func (*Faker) UUID

func (f *Faker) UUID() string

UUID returns a random v4 UUID string.

func (*Faker) Username

func (f *Faker) Username() string

Username returns a random username (lowercase first name + digits).

func (*Faker) Word

func (f *Faker) Word() string

Word returns a random common English word.

func (*Faker) ZipCode

func (f *Faker) ZipCode() string

ZipCode returns a random zip/postal code in a locale-appropriate format.

Jump to

Keyboard shortcuts

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