Categorygithub.com/go-faker/faker/v4
modulepackage
4.5.0
Repository: https://github.com/go-faker/faker.git
Documentation: pkg.go.dev

# README

Docs

faker

Struct Data Fake Generator

Faker will generate you a fake data based on your Struct.

Go Test codecov Go Report Card License Go.Dev

Index

Support

You can file an Issue. See documentation in Go.Dev

Getting Started

Download

go get -u github.com/go-faker/faker/v4

Example


DEMO


Example to use Faker

Benchmark


Bench To Generate Fake Data

Without Tag

BenchmarkFakerDataNOTTagged-4             500000              3049 ns/op             488 B/op         20 allocs/op

Using Tag

 BenchmarkFakerDataTagged-4                100000             17470 ns/op             380 B/op         26 allocs/op

MUST KNOW


The Struct Field must be PUBLIC.
Support Only For :

  • int, int8, int16, int32 & int64
  • []int, []int8, []int16, []int32 & []int64
  • bool & []bool
  • string & []string
  • float32, float64, []float32 &[]float64
  • time.Time & []time.Time
  • Nested Struct Field

Limitation


Unfortunately this library has some limitation

  • It does not support private fields. Make sure your structs fields you intend to generate fake data for are public, it would otherwise trigger a panic. You can however omit fields using a tag skip faker:"-" on your private fields.
  • It does not support the interface{} data type. How could we generate anything without knowing its data type?
  • It does not support the map[interface{}]interface{}, map[any_type]interface{} & map[interface{}]any_type data types. Once again, we cannot generate values for an unknown data type.
  • Custom types are not fully supported. However some custom types are already supported: we are still investigating how to do this the correct way. For now, if you use faker, it's safer not to use any custom types in order to avoid panics.
  • Some extra custom types can be supported IF AND ONLY IF extended with AddProvider() please see example
  • The oneof tag currently only supports string, the int types, and both float32 & float64. Further support is coming soon (i.e. hex numbers, etc). See example for usage.

Contribution


To contrib to this project, you can open a PR or an issue.

# Packages

No description provided by the author

# Functions

AddProvider extend faker with tag to generate fake data with specified custom algorithm Example: type Gondoruwo struct { Name string Locatadata int } type Sample struct { ID int64 `faker:"customIdFaker"` Gondoruwo Gondoruwo `faker:"gondoruwo"` Danger string `faker:"danger"` } func CustomGenerator() { // explicit faker.AddProvider("customIdFaker", func(v reflect.Value) (interface{}, error) { return int64(43), nil }) // functional faker.AddProvider("danger", func() faker.TaggedFunction { return func(v reflect.Value) (interface{}, error) { return "danger-ranger", nil } }()) faker.AddProvider("gondoruwo", func(v reflect.Value) (interface{}, error) { obj := Gondoruwo{ Name: "Power", Locatadata: 324, } return obj, nil }) } func main() { CustomGenerator() var sample Sample faker.FakeData(&sample) fmt.Printf("%+v", sample) } Will print {ID:43 Gondoruwo:{Name:Power Locatadata:324} Danger:danger-ranger} Notes: when using a custom provider make sure to return the same type as the field.
AmountWithCurrency get fake AmountWithCurrency USD 49257.100.
CCNumber get a credit card number randomly in string (VISA, MasterCard, etc).
CCType get a credit card type randomly in string (VISA, MasterCard, etc).
Century get century randomly in string.
ChineseFirstName get chinese first name.
ChineseLastName get chinese lsst name.
ChineseName get chinese lsst name.
Currency get fake Currency (IDR, USD).
Date get fake date in string randomly.
DayOfMonth get month randomly in string format.
DayOfWeek get day of week randomly in string format.
DomainName get email domain name in string.
E164PhoneNumber get fake E164PhoneNumber.
Email get email randomly in string.
FakeData is the main function.
FirstName get fake firstname.
FirstNameFemale get fake firstname for female.
FirstNameMale get fake firstname for male.
Gender get fake gender.
GetAddress returns a new Addresser interface of Address.
No description provided by the author
No description provided by the author
GetDateTimer returns a new DateTimer interface of DateTime.
GetIdentifier returns a new Identifier.
GetLorem returns a new DataFaker interface of Lorem struct.
GetNetworker returns a new Networker interface of Internet.
GetPayment returns a new Render interface of Payment struct.
GetPerson returns a new Dowser interface of Person struct.
GetPhoner serves as a constructor for Phoner interface.
GetPrice returns a new Money interface of Price struct.
GetRealAddress get real world address randomly.
No description provided by the author
IPv4 get IPv4 randomly in string.
IPv6 get IPv6 randomly in string.
Jwt get jwt-like string.
LastName get fake lastname.
Latitude get fake latitude randomly.
Longitude get fake longitude randomly.
MacAddress get mac address randomly in string.
MonthName get month name randomly in string format.
Name get fake name.
NewSafeSource wraps an unsafe rand.Source with a mutex to guard the random source against concurrent access.
Paragraph get a paragraph randomly in string.
Password get password randomly in string.
Phonenumber get fake phone number.
RandomInt Get three parameters , only first mandatory and the rest are optional (minimum_int, maximum_int, count) If only set one parameter : An integer greater than minimum_int will be returned If only set two parameters : All integers between minimum_int and maximum_int will be returned, in a random order.
RandomUnixTime is a helper function returning random Unix time.
RemoveProvider removes existing customization added with AddProvider.
ResetUnique is used to forget generated unique values.
Sentence get a sentence randomly in string.
SetCryptoSource sets a new reader for functions using a cryptographically-safe random generator (e.g.
SetRandomSource sets a new random source at the package level.
Timeperiod get timeperiod randomly in string (AM/PM).
Timestamp get timestamp randomly in string format: 2006-01-02 15:04:05.
TimeString get time randomly in string format.
Timezone get timezone randomly in string.
TitleFemale get a title female randomly in string ("Mrs.", "Ms.", "Miss", "Dr.", "Prof.", "Lady", "Queen", "Princess").
TitleMale get a title male randomly in string ("Mr.", "Dr.", "Prof.", "Lord", "King", "Prince").
TollFreePhoneNumber get fake TollFreePhoneNumber.
UnixTime get unix time randomly.
URL get Url randomly in string.
Username get username randomly in string.
UUIDDigit get fake Digit UUID.
UUIDHyphenated get fake Hyphenated UUID.
Word get a word randomly in string.
YearString get year randomly in string format.

# Constants

Supported tags.
Supported tags.
These example values must use the reference time "Mon Jan 2 15:04:05 MST 2006" as described at https://gobyexample.com/time-formatting-parsing.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
These example values must use the reference time "Mon Jan 2 15:04:05 MST 2006" as described at https://gobyexample.com/time-formatting-parsing.
These example values must use the reference time "Mon Jan 2 15:04:05 MST 2006" as described at https://gobyexample.com/time-formatting-parsing.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
These example values must use the reference time "Mon Jan 2 15:04:05 MST 2006" as described at https://gobyexample.com/time-formatting-parsing.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
These example values must use the reference time "Mon Jan 2 15:04:05 MST 2006" as described at https://gobyexample.com/time-formatting-parsing.
These example values must use the reference time "Mon Jan 2 15:04:05 MST 2006" as described at https://gobyexample.com/time-formatting-parsing.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
Supported tags.
These example values must use the reference time "Mon Jan 2 15:04:05 MST 2006" as described at https://gobyexample.com/time-formatting-parsing.

# Variables

PriorityTags define the priority order of the tag.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Address struct.
Internet struct.
No description provided by the author
DateTime struct.
Internet struct.
Lorem struct.
Payment struct.
Person struct.
Phone struct.
Price struct.
No description provided by the author
No description provided by the author
UserAgent implements logic loosely based on https://fakerphp.org/formatters/user-agent/.
UUID struct.

# Interfaces

Addresser is logical layer for Address.
No description provided by the author
DataFaker generates randomized Words, Sentences and Paragraphs.
A DateTimer contains random Time generators, returning time string in certain particular format.
Dowser provides interfaces to generate random logical Names with their initials.
Identifier ...
Money provides an interface to generate a custom price with or without a random currency code.
Networker is logical layer for Internet.
Phoner serves overall tele-phonic contact generator.
Render contains Whole Random Credit Card Generators with their types.
No description provided by the author