import"github.com/brianvoe/gofakeit/v6"gofakeit.Name() // Markus Moengofakeit.Email() // alaynawuckert@kozey.bizgofakeit.Phone() // (570)245-7485gofakeit.BS() // front-endgofakeit.BeerName() // Duvelgofakeit.Color() // MediumOrchidgofakeit.Company() // Moen, Pagac and Wuckertgofakeit.CreditCardNumber() // 4287271570245748gofakeit.HackerPhrase() // Connecting the array won't do anything, we need to generate the haptic COM driver!gofakeit.JobTitle() // Directorgofakeit.CurrencyShort() // USD
If you are using the default global usage and dont care about seeding no need to set anything. Gofakeit will seed it with a cryptographically secure number.
If you need a reproducible outcome you can set it via the Seed function call. Every example in this repo sets it for testing purposes.
import"github.com/brianvoe/gofakeit/v6"gofakeit.Seed(0) // If 0 will use crypto/rand to generate a number// orgofakeit.Seed(8675309) // Set it to whatever number you want
Random Sources
Gofakeit has a few rand sources, by default it uses math.Rand and uses mutex locking to allow for safe goroutines.
If you want to use a more performant source please use NewUnlocked. Be aware that it is not goroutine safe.
import"github.com/brianvoe/gofakeit/v6"// Uses math/rand(Pseudo) with mutex lockingfaker := gofakeit.New(0)// Uses math/rand(Pseudo) with NO mutext locking// More performant but not goroutine safe.faker := gofakeit.NewUnlocked(0)// Uses crypto/rand(cryptographically secure) with mutext lockingfaker := gofakeit.NewCrypto()// Pass in your own random sourcefaker := gofakeit.NewCustom()
Global Rand Set
If you would like to use the simple function calls but need to use something like crypto/rand you can override the default global with the random source that you want.
Gofakeit can generate random data for struct fields. For the most part it covers all the basic type as well as some non basic like time.Time.
Struct fields can also use tags to more specifically generate data for that field type.
import"github.com/brianvoe/gofakeit/v6"// Create structs with random injected datatypeFoostruct { Str 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 Map map[string]int`fakesize:"2"` Array []string`fakesize:"2"` Bar Bar 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"`}typeBarstruct { Name string Number int Float float32}// Pass your struct as a pointervar f Foogofakeit.Struct(&f)fmt.Println(f.Str) // hrukpttuezptneuvunhfmt.Println(f.Int) // -7825289004089916589fmt.Println(*f.Pointer) // -343806609094473732fmt.Println(f.Name) // fredfmt.Println(f.Sentence) // Record river mind.fmt.Println(f.RandStr) // worldfmt.Println(f.Number) // 4fmt.Println(f.Regex) // cbdfcfmt.Println(f.Map) // map[PxLIo:52 lxwnqhqc:846]fmt.Println(f.Array) // cbdfcfmt.Printf("%+v", f.Bar) // {Name:QFpZ Number:-2882647639396178786 Float:1.7636692e+37}fmt.Println(f.Skip) // <nil>fmt.Println(f.Created.String()) // 1908-12-07 04:14:25.685339029 +0000 UTC// Supported formats// int, int8, int16, int32, int64,// uint, uint8, uint16, uint32, uint64,// float32, float64,// bool, string,// array, pointers, map// time.Time // If setting time you can also set a format tag// Nested Struct Fields and Embeded Fields
Custom Functions
In a lot of sitations you may need to use your own random function usage for your specific needs.
If you would like to extend the usage of struct tags, generate function, available usages in the gofakeit server or gofakeit command sub packages. You can do so via the AddFuncLookup. Each function has their own lookup, if you need more reference examples you can look at each files lookups.
// Simplegofakeit.AddFuncLookup("friendname", Info{ Category: "custom", Description: "Random friend name", Example: "bill", Output: "string", Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {returnRandomString([]string{"bill", "bob", "sally"}), nil },})// With Paramsgofakeit.AddFuncLookup("jumbleword", Info{ Category: "jumbleword", Description: "Take a word and jumple it up", Example: "loredlowlh", Output: "string", Params: []Param{ {Field: "word", Type: "string", 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 } split := strings.Split(word, "")ShuffleStrings(split)return strings.Join(split, ""), nil },})typeFoostruct { FriendName string`fake:"{friendname}"` JumbleWord string`fake:"{jumbleword:helloworld}"`}var f Foogofakeit.Struct(&f)fmt.Printf("%s", f.FriendName) // billfmt.Printf("%s", f.JumbleWord) // loredlowlh
Functions
All functions also exist as methods on the Faker struct