gofakeit is a Random data generator written in go.
It can fake Random data about File, Person, Generator, Auth, Address, Game, Beer, Car, Words, Foods, Misc, Colors, Internat, Date/Time, Payment, Company, Hacker, Hipster, App, Animal, Emoji, Language, Number, String, and custom Function, etc.
Installation
Standard go get:
1
$ go get -u github.com/brianvoe/gofakeit/v6
Next, include it in your application:
1
import"github.com/brianvoe/gofakeit/v6"
Usage
Simple Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import"github.com/brianvoe/gofakeit/v6"
gofakeit.Seed(0)
gofakeit.Name() // Markus Moen gofakeit.Email() // [email protected] gofakeit.Phone() // (570)245-7485 gofakeit.BS() // front-end gofakeit.BeerName() // Duvel gofakeit.Color() // MediumOrchid gofakeit.Company() // Moen, Pagac and Wuckert gofakeit.CreditCardNumber() // 4287271570245748 gofakeit.HackerPhrase() // Connecting the array won't do anything, we need to generate the haptic COM driver! gofakeit.JobTitle() // Director gofakeit.CurrencyShort() // USD // See full list below
Concurrent Struct
If you need to have independent randomization for the purposes of concurrency
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import"github.com/brianvoe/gofakeit/v6"
faker := New(0) // or NewCrypto() to use crypto/rand
faker.Name() // Markus Moen faker.Email() // [email protected] faker.Phone() // (570)245-7485 faker.BS() // front-end faker.BeerName() // Duvel faker.Color() // MediumOrchid faker.Company() // Moen, Pagac and Wuckert faker.CreditCardNumber() // 4287271570245748 faker.HackerPhrase() // Connecting the array won't do anything, we need to generate the haptic COM driver! faker.JobTitle() // Director faker.CurrencyShort() // USD // See full list below
Global Rand Set
If you would like to use the simple function call but need to use something like crypto/rand you can override the default global with the type you want
// Create structs with random injected data type Foo struct { Bar string Int int Pointer *int Name string`fake:"{firstname}"`// Any available function all lowercase Sentence string`fake:"{sentence:3}"`// Can call with parameters RandStr string`fake:"{randomstring:[hello,world]}"` Number string`fake:"{number:1,10}"`// Comma separated for multiple values Regex string`fake:"{regex:[abcdef]{5}}"`// Generate string from regex Skip *string`fake:"skip"`// Set to "skip" to not generate data for Created time.Time // Can take in a fake tag as well as a format tag CreatedFormat time.Time `fake:"{year}-{month}-{day}" format:"2006-01-02"` }
type FooBar struct { Bars []string`fake:"{name}"`// Array of random size (1-10) with fake function applied Foos []Foo `fakesize:"3"`// Array of size specified with faked struct FooBars []Foo `fake:"{name}" fakesize:"3"`// Array of size 3 with fake function applied }
// Pass your struct as a pointer var f Foo gofakeit.Struct(&f) fmt.Println(f.Bar) // hrukpttuezptneuvunh fmt.Println(f.Int) // -7825289004089916589 fmt.Println(*f.Pointer) // -343806609094473732 fmt.Println(f.Name) // fred fmt.Println(f.Sentence) // Record river mind. fmt.Println(f.RandStr) // world fmt.Println(f.Number) // 4 fmt.Println(f.Regex) // cbdfc fmt.Println(f.Skip) // <nil> fmt.Println(f.Created.String()) // 1908-12-07 04:14:25.685339029 +0000 UTC
// Supported formats - Array and pointers as well // int, int8, int16, int32, int64, // float32, float64, // bool, string, // time.Time // If setting time you can also set a format tag // Nested Struct Fields and Embeded Fields
// With Params AddFuncLookup("jumbleword", Info{ Category: "jumbleword", Description: "Take a word and jumple it up", Example: "loredlowlh", Output: "string", Params: []Param{ {Field: "word", Type: "int", Description: "Word you want to jumble"}, }, Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) { word, err := info.GetString(m, "word") if err != nil { returnnil, err }