Documentation
¶
Overview ¶
Package faker is a pure-Go (CGO=0) port of the Ruby `faker` gem: a seeded, MRI-faithful fake-data generator.
Deterministic seeding contract ¶
The gem's key correctness property is that, given
Faker::Config.random = Random.new(SEED)
the sequence of generated values is reproducible. This package mirrors that: construct a Faker with New over a Random seeded by NewRandom and the draw sequence reproduces MRI's, because Random is a bit-exact port of Ruby's MT19937 (including init_by_array seeding and the mask-and-reject integer scheme) and the Array#sample / Array#shuffle helpers replay MRI's algorithms.
Seed-parity scope (honest) ¶
Exact-sequence parity with the gem holds for every generator whose randomness flows through INTEGER draws and single/sampled selections: Name, Address (city/street/zip/state/country/full_address), PhoneNumber, Lorem, Company, Commerce.product_name/department, Color.color_name, Boolean, Number's integer paths (number/digit/hexadecimal/binary/decimal), Internet's table-driven paths, and Date.between/forward/backward (Julian-day integer arithmetic).
Distribution/format parity only (NOT bit-exact sequence) applies where the value derives from a FLOAT-range draw, because MRI's Random#rand(lo..hi) for float ranges uses a higher-precision internal algorithm this port does not reproduce: Address.latitude/longitude, Number.between/within/positive/ negative/normal with non-integer bounds, Commerce.price, and Time.between/ forward/backward. These match the gem's data tables, formats and statistical distribution; their exact per-seed value may differ in low-order digits.
Index ¶
- type AddressGen
- func (a *AddressGen) BuildingNumber() string
- func (a *AddressGen) City() string
- func (a *AddressGen) CityPrefix() string
- func (a *AddressGen) CitySuffix() string
- func (a *AddressGen) Community() string
- func (a *AddressGen) Country() string
- func (a *AddressGen) CountryCode() string
- func (a *AddressGen) CountryCodeLong() string
- func (a *AddressGen) FullAddress() string
- func (a *AddressGen) Latitude() float64
- func (a *AddressGen) Longitude() float64
- func (a *AddressGen) Postcode() string
- func (a *AddressGen) SecondaryAddress() string
- func (a *AddressGen) State() string
- func (a *AddressGen) StateAbbr() string
- func (a *AddressGen) StreetAddress() string
- func (a *AddressGen) StreetName() string
- func (a *AddressGen) StreetSuffix() string
- func (a *AddressGen) TimeZone() string
- func (a *AddressGen) Zip() string
- func (a *AddressGen) ZipCode() string
- type BooleanGen
- type ColorGen
- type CommerceGen
- type CompanyGen
- type DateGen
- type Faker
- func (f *Faker) Address() *AddressGen
- func (f *Faker) Boolean() *BooleanGen
- func (f *Faker) Bothify(s string) string
- func (f *Faker) Color() *ColorGen
- func (f *Faker) Commerce() *CommerceGen
- func (f *Faker) Company() *CompanyGen
- func (f *Faker) Date() *DateGen
- func (f *Faker) Fetch(key string) string
- func (f *Faker) FetchAll(key string) []string
- func (f *Faker) Internet() *InternetGen
- func (f *Faker) Letterify(s string) string
- func (f *Faker) Locale() string
- func (f *Faker) Lorem() *LoremGen
- func (f *Faker) Name() *NameGen
- func (f *Faker) Number() *NumberGen
- func (f *Faker) Numerify(s string, leadingZero bool) string
- func (f *Faker) Parse(key string) string
- func (f *Faker) PhoneNumber() *PhoneNumberGen
- func (f *Faker) Random() *Random
- func (f *Faker) Regexify(reg string) string
- func (f *Faker) Sample(list []string) string
- func (f *Faker) SampleN(list []string, n int) []string
- func (f *Faker) SetLocale(loc string)
- func (f *Faker) SetRandom(r *Random)
- func (f *Faker) Shuffle(list []string) []string
- func (f *Faker) Time() *TimeGen
- type InternetGen
- func (in *InternetGen) DomainName() string
- func (in *InternetGen) DomainSuffix() string
- func (in *InternetGen) DomainWord() string
- func (in *InternetGen) Email() string
- func (in *InternetGen) IPv4Address() string
- func (in *InternetGen) IPv6Address() string
- func (in *InternetGen) MacAddress(prefix string) string
- func (in *InternetGen) Password(minLength, maxLength int, mixCase, specialChars bool) string
- func (in *InternetGen) SafeDomainSuffix() string
- func (in *InternetGen) Slug() string
- func (in *InternetGen) URL() string
- func (in *InternetGen) Username() string
- type LoremGen
- func (l *LoremGen) Characters(number int) string
- func (l *LoremGen) Paragraph(sentenceCount int, supplemental bool) string
- func (l *LoremGen) Paragraphs(number int, supplemental bool) []string
- func (l *LoremGen) Sentence(wordCount int, supplemental bool) string
- func (l *LoremGen) Sentences(number int, supplemental bool) []string
- func (l *LoremGen) Word() string
- func (l *LoremGen) Words(number int, supplemental bool) []string
- type NameGen
- func (n *NameGen) FemaleFirstName() string
- func (n *NameGen) FirstName() string
- func (n *NameGen) Initials(number int) string
- func (n *NameGen) LastName() string
- func (n *NameGen) MaleFirstName() string
- func (n *NameGen) Name() string
- func (n *NameGen) NameWithMiddle() string
- func (n *NameGen) NeutralFirstName() string
- func (n *NameGen) Prefix() string
- func (n *NameGen) Suffix() string
- type NumberGen
- func (n *NumberGen) Between(from, to float64) float64
- func (n *NumberGen) BetweenInt(from, to int) int
- func (n *NumberGen) Binary(digits int) string
- func (n *NumberGen) Decimal(lDigits, rDigits int) float64
- func (n *NumberGen) Digit() int
- func (n *NumberGen) Hexadecimal(digits int) string
- func (n *NumberGen) Negative(from, to float64) float64
- func (n *NumberGen) NonZeroDigit() int
- func (n *NumberGen) Normal(mean, stddev float64) float64
- func (n *NumberGen) Number(digits int) int64
- func (n *NumberGen) Positive(from, to float64) float64
- type PhoneNumberGen
- func (p *PhoneNumberGen) AreaCode() string
- func (p *PhoneNumberGen) CellPhone() string
- func (p *PhoneNumberGen) CellPhoneWithCountryCode() string
- func (p *PhoneNumberGen) CountryCode() string
- func (p *PhoneNumberGen) ExchangeCode() string
- func (p *PhoneNumberGen) Extension(length int) string
- func (p *PhoneNumberGen) PhoneNumber() string
- func (p *PhoneNumberGen) PhoneNumberWithCountryCode() string
- func (p *PhoneNumberGen) SubscriberNumber(length int) string
- type Random
- type RetriesExhaustedError
- type TimeGen
- type Unique
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AddressGen ¶
type AddressGen struct {
// contains filtered or unexported fields
}
AddressGen is Faker::Address.
func (*AddressGen) BuildingNumber ¶
func (a *AddressGen) BuildingNumber() string
BuildingNumber produces a building number (Faker::Address.building_number).
func (*AddressGen) City ¶
func (a *AddressGen) City() string
City produces a city name (Faker::Address.city).
func (*AddressGen) CityPrefix ¶
func (a *AddressGen) CityPrefix() string
CityPrefix produces a city prefix (Faker::Address.city_prefix).
func (*AddressGen) CitySuffix ¶
func (a *AddressGen) CitySuffix() string
CitySuffix produces a city suffix (Faker::Address.city_suffix).
func (*AddressGen) Community ¶
func (a *AddressGen) Community() string
Community produces a community name (Faker::Address.community).
func (*AddressGen) Country ¶
func (a *AddressGen) Country() string
Country produces a country name (Faker::Address.country).
func (*AddressGen) CountryCode ¶
func (a *AddressGen) CountryCode() string
CountryCode produces an ISO 3166 alpha-2 country code (Faker::Address.country_code).
func (*AddressGen) CountryCodeLong ¶
func (a *AddressGen) CountryCodeLong() string
CountryCodeLong produces an ISO 3166 alpha-3 country code.
func (*AddressGen) FullAddress ¶
func (a *AddressGen) FullAddress() string
FullAddress produces a full address (Faker::Address.full_address).
func (*AddressGen) Latitude ¶
func (a *AddressGen) Latitude() float64
Latitude produces a latitude in [-90, 90). Distribution parity only (float draw).
func (*AddressGen) Longitude ¶
func (a *AddressGen) Longitude() float64
Longitude produces a longitude in [-180, 180). Distribution parity only.
func (*AddressGen) Postcode ¶
func (a *AddressGen) Postcode() string
Postcode is an alias for ZipCode.
func (*AddressGen) SecondaryAddress ¶
func (a *AddressGen) SecondaryAddress() string
SecondaryAddress produces a secondary address (Faker::Address.secondary_address).
func (*AddressGen) State ¶
func (a *AddressGen) State() string
State produces a state name (Faker::Address.state).
func (*AddressGen) StateAbbr ¶
func (a *AddressGen) StateAbbr() string
StateAbbr produces a state abbreviation (Faker::Address.state_abbr).
func (*AddressGen) StreetAddress ¶
func (a *AddressGen) StreetAddress() string
StreetAddress produces a street address (Faker::Address.street_address).
func (*AddressGen) StreetName ¶
func (a *AddressGen) StreetName() string
StreetName produces a street name (Faker::Address.street_name).
func (*AddressGen) StreetSuffix ¶
func (a *AddressGen) StreetSuffix() string
StreetSuffix produces a street suffix (Faker::Address.street_suffix).
func (*AddressGen) TimeZone ¶
func (a *AddressGen) TimeZone() string
TimeZone produces a time-zone name (Faker::Address.time_zone).
func (*AddressGen) ZipCode ¶
func (a *AddressGen) ZipCode() string
ZipCode produces a zip/postal code (Faker::Address.zip_code). The fetched postcode pattern is letterified then numerified with leading zeros allowed.
type BooleanGen ¶
type BooleanGen struct {
// contains filtered or unexported fields
}
BooleanGen is Faker::Boolean.
func (*BooleanGen) Boolean ¶
func (b *BooleanGen) Boolean(trueRatio float64) bool
Boolean returns a bool that is true with probability trueRatio (Faker::Boolean.boolean). trueRatio 0.5 gives an even coin.
type ColorGen ¶
type ColorGen struct {
// contains filtered or unexported fields
}
ColorGen is Faker::Color.
type CommerceGen ¶
type CommerceGen struct {
// contains filtered or unexported fields
}
CommerceGen is Faker::Commerce.
func (*CommerceGen) Department ¶
func (c *CommerceGen) Department(max int, fixedAmount bool) string
Department produces a department name. With fixedAmount the number of categories is exactly max; otherwise it is 1 + rand(max) (Faker::Commerce.department). Multiple categories are joined "a, b & c".
func (*CommerceGen) Price ¶
func (c *CommerceGen) Price(max float64) float64
Price produces a price in the half-open range [0, max), floored to cents (Faker::Commerce.price). Distribution parity only (float-range draw).
func (*CommerceGen) ProductName ¶
func (c *CommerceGen) ProductName() string
ProductName produces "Adjective Material Product" (Faker::Commerce.product_name).
type CompanyGen ¶
type CompanyGen struct {
// contains filtered or unexported fields
}
CompanyGen is Faker::Company.
func (*CompanyGen) BS ¶
func (c *CompanyGen) BS() string
BS produces some company BS, one word from each bs column (Faker::Company.bs).
func (*CompanyGen) Buzzword ¶
func (c *CompanyGen) Buzzword() string
Buzzword produces a single buzzword (Faker::Company.buzzword).
func (*CompanyGen) CatchPhrase ¶
func (c *CompanyGen) CatchPhrase() string
CatchPhrase produces a company catch phrase by sampling one word from each buzzword column (Faker::Company.catch_phrase).
func (*CompanyGen) Industry ¶
func (c *CompanyGen) Industry() string
Industry produces a company industry (Faker::Company.industry).
func (*CompanyGen) Name ¶
func (c *CompanyGen) Name() string
Name produces a company name (Faker::Company.name).
func (*CompanyGen) Suffix ¶
func (c *CompanyGen) Suffix() string
Suffix produces a company suffix (Faker::Company.suffix).
type DateGen ¶
type DateGen struct {
// contains filtered or unexported fields
}
DateGen is Faker::Date. Date arithmetic uses whole-day (Julian-day) integer draws, so DateGen is bit-exact with the gem's seeded sequence.
func (*DateGen) Backward ¶
Backward returns a date 1..days in the past from base (Faker::Date.backward).
type Faker ¶
type Faker struct {
// contains filtered or unexported fields
}
Faker holds the configuration (locale + RNG) and exposes the generators. It corresponds to Faker::Config plus the Faker::Base class methods.
func (*Faker) Address ¶
func (f *Faker) Address() *AddressGen
Address returns the Faker::Address generator.
func (*Faker) Boolean ¶
func (f *Faker) Boolean() *BooleanGen
Boolean returns the Faker::Boolean generator.
func (*Faker) Commerce ¶
func (f *Faker) Commerce() *CommerceGen
Commerce returns the Faker::Commerce generator.
func (*Faker) Company ¶
func (f *Faker) Company() *CompanyGen
Company returns the Faker::Company generator.
func (*Faker) Fetch ¶
Fetch mirrors Faker::Base.fetch: sample one value from a locale array; if the chosen value is a /regex/ literal, regexify it.
func (*Faker) Internet ¶
func (f *Faker) Internet() *InternetGen
Internet returns the Faker::Internet generator.
func (*Faker) Letterify ¶
Letterify mirrors Faker::Base.letterify: each '?' becomes an uppercase A–Z.
func (*Faker) Numerify ¶
Numerify mirrors Faker::Base.numerify: each '#' becomes a digit. By default the FIRST '#' becomes 1..9 (no leading zero) and the rest 0..9; with leadingZero true every '#' becomes 0..9.
func (*Faker) Parse ¶
Parse mirrors Faker::Base.parse: fetch a format string from the locale and expand its #{token} placeholders by dispatching to generators (Name., Address., …) or recursive locale lookups. If nothing parses, the string is numerified.
func (*Faker) PhoneNumber ¶
func (f *Faker) PhoneNumber() *PhoneNumberGen
PhoneNumber returns the Faker::PhoneNumber generator.
func (*Faker) Regexify ¶
Regexify mirrors Faker::Base.regexify: a deliberately simple regex generator matching the gem's supported subset (anchors, {n}/{m,n}, ?, [...] classes and ranges, (a|b) alternations, \d, \w). It draws with the same Array#sample over the configured RNG, so seeded output matches the gem for the patterns it supports.
func (*Faker) SetLocale ¶
SetLocale sets the locale. Only "en" ships embedded data; other locales fall back to "en" (mirroring the gem's en fallback for missing translations).
type InternetGen ¶
type InternetGen struct {
// contains filtered or unexported fields
}
InternetGen is Faker::Internet.
func (*InternetGen) DomainName ¶
func (in *InternetGen) DomainName() string
DomainName produces "word.suffix" using a safe suffix (Faker::Internet.domain_name).
func (*InternetGen) DomainSuffix ¶
func (in *InternetGen) DomainSuffix() string
DomainSuffix produces a domain suffix (Faker::Internet.domain_suffix).
func (*InternetGen) DomainWord ¶
func (in *InternetGen) DomainWord() string
DomainWord produces the leading word of a domain (Faker::Internet.domain_word): the prepared first token of a company name.
func (*InternetGen) Email ¶
func (in *InternetGen) Email() string
Email produces an email address (Faker::Internet.email): a username local part at a generated domain.
func (*InternetGen) IPv4Address ¶
func (in *InternetGen) IPv4Address() string
IPv4Address produces a dotted IPv4 address (Faker::Internet.ip_v4_address).
func (*InternetGen) IPv6Address ¶
func (in *InternetGen) IPv6Address() string
IPv6Address produces a colon-separated IPv6 address (Faker::Internet.ip_v6_address).
func (*InternetGen) MacAddress ¶
func (in *InternetGen) MacAddress(prefix string) string
MacAddress produces a colon-separated MAC address (Faker::Internet.mac_address). prefix may be a partial "aa" or "aa:bb" hex prefix.
func (*InternetGen) Password ¶
func (in *InternetGen) Password(minLength, maxLength int, mixCase, specialChars bool) string
Password produces a password between minLength and maxLength characters (Faker::Internet.password). With mixCase an uppercase letter is guaranteed; with specialChars a special character is guaranteed. The result is shuffled.
func (*InternetGen) SafeDomainSuffix ¶
func (in *InternetGen) SafeDomainSuffix() string
SafeDomainSuffix produces a safe domain suffix ("example"/"test").
func (*InternetGen) Slug ¶
func (in *InternetGen) Slug() string
Slug produces a URL slug of two words joined by '-' or '_' (Faker::Internet.slug).
func (*InternetGen) URL ¶
func (in *InternetGen) URL() string
URL produces "http://domain/username" (Faker::Internet.url).
func (*InternetGen) Username ¶
func (in *InternetGen) Username() string
Username produces a username (Faker::Internet.username with no specifier).
Faithful to the gem's sample over two freshly-evaluated options:
sample([
Char.prepare(Faker::Name.first_name),
[Faker::Name.first_name, Faker::Name.last_name].map { Char.prepare }.join(sample(separators)),
])
so the draw sequence is first_name (option 0), then first_name + last_name + sample(separators) (option 1), then the 2-element sample, then downcase. Note the two options each draw their OWN first_name — they are not shared.
type LoremGen ¶
type LoremGen struct {
// contains filtered or unexported fields
}
LoremGen is Faker::Lorem.
func (*LoremGen) Characters ¶
Characters returns number random alphanumeric characters (Faker::Lorem.characters), drawing from a-z and 0-9.
func (*LoremGen) Paragraph ¶
Paragraph returns a paragraph of sentenceCount sentences joined by spaces.
func (*LoremGen) Paragraphs ¶
Paragraphs returns number paragraphs (each of 3 sentences).
func (*LoremGen) Sentence ¶
Sentence returns a capitalized sentence of wordCount words ended with the locale period.
func (*LoremGen) Sentences ¶
Sentences returns number sentences (each of 3 words, matching the gem).
type NameGen ¶
type NameGen struct {
// contains filtered or unexported fields
}
NameGen is Faker::Name.
func (*NameGen) FemaleFirstName ¶
FemaleFirstName produces a random female first name.
func (*NameGen) FirstName ¶
FirstName produces a random first name.
This mirrors a subtle quirk of the gem: Faker::Name.first_name evaluates parse('name.first_name') TWICE — once for the `.empty?` guard and again to produce the return value — so it consumes two parse cycles' worth of draws. Reproducing that double evaluation is required for seed-exact parity.
func (*NameGen) MaleFirstName ¶
MaleFirstName produces a random male first name.
func (*NameGen) NameWithMiddle ¶
NameWithMiddle produces a name with a middle name.
func (*NameGen) NeutralFirstName ¶
NeutralFirstName produces a random gender-neutral first name.
type NumberGen ¶
type NumberGen struct {
// contains filtered or unexported fields
}
NumberGen is Faker::Number.
func (*NumberGen) Between ¶
Between returns a float in [from, to] (inclusive). NOTE: float-range distribution parity only, not bit-exact sequence (see package doc).
func (*NumberGen) BetweenInt ¶
BetweenInt returns an integer in [from, to] (inclusive) — bit-exact with the gem when both bounds are integers.
func (*NumberGen) Binary ¶
Binary produces a binary string of the given length (Faker::Number.binary).
func (*NumberGen) Decimal ¶
Decimal produces a float with lDigits integer digits and rDigits fractional digits (Faker::Number.decimal). The last fractional digit is non-zero.
func (*NumberGen) Hexadecimal ¶
Hexadecimal produces a lowercase hex string of the given length (Faker::Number.hexadecimal).
func (*NumberGen) Negative ¶
Negative returns a negative float in the range (Faker::Number.negative).
func (*NumberGen) NonZeroDigit ¶
NonZeroDigit produces a non-zero single-digit integer 1..9.
func (*NumberGen) Normal ¶
Normal returns a Gaussian float given mean and standard deviation (Faker::Number.normal, Box–Muller). Distribution parity only.
type PhoneNumberGen ¶
type PhoneNumberGen struct {
// contains filtered or unexported fields
}
PhoneNumberGen is Faker::PhoneNumber.
func (*PhoneNumberGen) AreaCode ¶
func (p *PhoneNumberGen) AreaCode() string
AreaCode produces an area code (Faker::PhoneNumber.area_code).
func (*PhoneNumberGen) CellPhone ¶
func (p *PhoneNumberGen) CellPhone() string
CellPhone produces a cell phone number (Faker::PhoneNumber.cell_phone).
func (*PhoneNumberGen) CellPhoneWithCountryCode ¶
func (p *PhoneNumberGen) CellPhoneWithCountryCode() string
CellPhoneWithCountryCode prefixes a country code to a cell number.
func (*PhoneNumberGen) CountryCode ¶
func (p *PhoneNumberGen) CountryCode() string
CountryCode produces "+NN" (Faker::PhoneNumber.country_code).
func (*PhoneNumberGen) ExchangeCode ¶
func (p *PhoneNumberGen) ExchangeCode() string
ExchangeCode produces an exchange code (Faker::PhoneNumber.exchange_code).
func (*PhoneNumberGen) Extension ¶
func (p *PhoneNumberGen) Extension(length int) string
Extension is an alias for SubscriberNumber.
func (*PhoneNumberGen) PhoneNumber ¶
func (p *PhoneNumberGen) PhoneNumber() string
PhoneNumber produces a phone number in a random format (Faker::PhoneNumber.phone_number).
func (*PhoneNumberGen) PhoneNumberWithCountryCode ¶
func (p *PhoneNumberGen) PhoneNumberWithCountryCode() string
PhoneNumberWithCountryCode prefixes a country code (Faker::PhoneNumber.phone_number_with_country_code).
func (*PhoneNumberGen) SubscriberNumber ¶
func (p *PhoneNumberGen) SubscriberNumber(length int) string
SubscriberNumber produces a numeric subscriber number of the given length (Faker::PhoneNumber.subscriber_number / extension). It mirrors the gem's PositionalGenerator int(length:): a single range [10^(n-1), 10^n - 1] is sampled from a one-element array (consuming one draw) and then a value in that inclusive range is drawn.
type Random ¶
type Random struct {
// contains filtered or unexported fields
}
Random is Ruby's Random: an MT19937 generator seeded exactly as MRI does (init_by_array over the seed's 32-bit little-endian words), so seeded output matches MRI bit for bit. This is the keystone of Faker's deterministic-seed contract: NewRandom(seed) reproduces the same draw sequence MRI's Random.new(seed) produces.
func NewRandomEntropy ¶
func NewRandomEntropy() *Random
NewRandomEntropy draws a non-deterministic seed (matching MRI's entropy-seeded Random.new with no argument) and returns a seeded Random.
func (*Random) Bytes ¶
Bytes returns n pseudo-random bytes, matching Random#bytes(n) (little-endian words from genrand_int32).
func (*Random) FloatInRange ¶
FloatInRange returns lo + res53*(hi-lo). NOTE: this is an approximation of MRI's Random#rand(lo..hi) for FLOAT ranges; MRI uses a higher-precision internal float-range algorithm that this does not bit-match. Integer-range draws (IntInRange) ARE bit-exact; see the package doc for the seed-parity scope.
func (*Random) FloatScaled ¶
FloatScaled mirrors Random#rand(f) for a positive float f: res53 * f.
func (*Random) IntInRange ¶
IntInRange mirrors Random#rand(lo..hi) (inclusive) over integers.
type RetriesExhaustedError ¶
type RetriesExhaustedError struct {
Retries int
}
RetriesExhaustedError is returned (as the error value) when Unique cannot find a fresh value within the retry budget, mirroring the gem's Faker::UniqueGenerator::RetryLimitExceeded.
func (*RetriesExhaustedError) Error ¶
func (e *RetriesExhaustedError) Error() string
type TimeGen ¶
type TimeGen struct {
// contains filtered or unexported fields
}
TimeGen is Faker::Time. Time draws use float ranges, so TimeGen matches the gem's data/format/distribution but NOT its exact per-seed value (see package doc on seed-parity scope).
type Unique ¶
type Unique struct {
// contains filtered or unexported fields
}
Unique wraps a generator function so each returned value is distinct, mirroring the gem's `Faker::Name.unique.first_name` modifier. It calls gen until it produces a value not seen before, giving up after maxRetries attempts.
Pass maxRetries <= 0 to use the gem's default of 10_000.
