[Awesome Go] Use gofakeit package to generate random fake data

gofakeit

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

1
2
3
4
import "github.com/brianvoe/gofakeit/v6"

faker := NewCrypto()
SetGlobalFaker(faker)

Struct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import "github.com/brianvoe/gofakeit/v6"

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

var fb FooBar
gofakeit.Struct(&fb)
fmt.Println(fb.Bars) // [Charlie Senger]
fmt.Println(fb.Foos) // [{blmfxy -2585154718894894116 0xc000317bc0 Emmy Attitude demand addition. hello 3 <nil>} {cplbf -1722374676852125164 0xc000317cb0 Viva Addition option link. hello 7 <nil>}]

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

Custom Functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Simple
AddFuncLookup("friendname", Info{
Category: "custom",
Description: "Random friend name",
Example: "bill",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
return RandomString([]string{"bill", "bob", "sally"}), nil
},
})

// 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 {
return nil, err
}

split := strings.Split(word, "")
ShuffleStrings(split)
return strings.Join(split, ""), nil
},
})

type Foo struct {
FriendName string `fake:"{friendname}"`
JumbleWord string `fake:"{jumbleword:helloworld}"`
}

var f Foo
Struct(&f)
fmt.Printf("%s", f.FriendName) // bill
fmt.Printf("%s", f.JumbleWord) // loredlowlh

Functions

All functions also exist as methods on the Faker struct

FAQs

runtime: goroutine stack exceeds 1000000000-byte limit. fatal error: stack overflow

gofakeit fake the circular references between structs will cause an error goroutine stack exceeds limit.

1
2
3
4
5
6
7
8
9
10
type A struct {
B B
}

type B struct {
A A
}

var a A
gofakeit.Struct(&a) // It will cause an error: fatal error: stack overflow.

We can use fake:"skip" to skip circular references.

1
2
3
4
5
6
7
type A struct {
B B `fake:"skip"`
}

type B struct {
A A `fake:"skip"`
}

length limit or less than

1
2
3
type Data struct {
Description string `fake:"{sentence:255}"` // It will cause an error about must be greater than
}

Some fake data have length limit. You can see less than - https://github.com/brianvoe/gofakeit/search?q=less+thann to learn more.

We can reduce length.

1
2
3
type Data struct {
Description string `fake:"{sentence:32}"`
}

References

[1] GitHub - brianvoe/gofakeit: Random fake data generator written in go - https://github.com/brianvoe/gofakeit