DEV Community

Cover image for Increase Your Productivity With Dummy Data Generator in Golang
luthfisauqi17
luthfisauqi17

Posted on

Increase Your Productivity With Dummy Data Generator in Golang

Lately, I have been building a few “fun” projects using Go programming language. A productivity concern for me while building the project is when it comes to making some dummy data. For example, let say I want to generate two dummy data for struct Product...

type Product struct {
    name        string
    description string
}

dummyProduct1 := Product{
    "Aqua Clock Matrix",
    "The decription of Aqua Clock Matrix.",
}
Enter fullscreen mode Exit fullscreen mode

… there you go, I have no problem with that. Let’s say I want to make 5 dummy data of the same struct…

type Product struct {
    name        string
    description string
}

dummyProduct1 := Product{
    "Aqua Clock Matrix",
    "The decription of Aqua Clock Matrix.",
}

dummyProduct2 := Product{
    "Sharp Toaster Matrix",
    "The decription of Sharp Toaster Matrix.",
}

dummyProduct3 := Product{
    "Drone Turbo Paper",
    "The decription of Drone Turbo Paper.",
}

dummyProduct4 := Product{
    "Black Wood Lamp",
    "The description of Black Wood Lamp",
}

dummyProduct5 := Product{
    "Zoom Swift Toy",
    "The description of Zoom Swift Toy",
}
Enter fullscreen mode Exit fullscreen mode

… there also no problem for me to make those dummy data. But imagine if I need to make hundreds or thousands of dummy data (also with real life data description), it would be very tiring! But worry no more as I found a very useful library to generate random data with ease.


Go Fake It

gofakeit_logo

Lucky, you can use Go library called gofakeit to perform random data generation task, checkout its repository at this link. This library has many functions you can use it depends on your need, including:

  • Product
  • Person
  • Address
  • Car
  • Words
  • Foods
  • Colors
  • Internet
  • Company
  • Animal

For complete available functions, you can check this following link.


Implementation

To use this library, you can install it into your development environment:

go get github.com/brianvoe/gofakeit/v6
Enter fullscreen mode Exit fullscreen mode

After installing the library, you can start use it.

Let’s implement the library to my Product struct:

type Product struct {
    name        string
    description string
}

fakeProduct := gofakeit.Product()

dummyProduct := Product{
    fakeProduct.Name,
    fakeProduct.Description,
}

fmt.Printf("%#v\n", dummyProduct)
Enter fullscreen mode Exit fullscreen mode

If you run the above code, you will get the following result:

main.Product{name:"yellow quartz hair dryer", description:"Wisp outcome caravan just terse why it generally their nothing caused nothing next jittery. Comb regiment child yearly mine art inquire hardly."}
Enter fullscreen mode Exit fullscreen mode

No worries if I need 20 dummy product data, you can do the following:

type Product struct {
    name        string
    description string
}

var dummyProducts []Product

for i := 0; i < 20; i++ {

    fakeProduct := gofakeit.Product()

    dummyProduct := Product{
        fakeProduct.Name,
        fakeProduct.Description,
    }

    dummyProducts = append(dummyProducts, dummyProduct)
}

for index, dummyProduct := range dummyProducts {

    fmt.Printf("Dummy product #%v - %#v\n", index+1, dummyProduct)
}
Enter fullscreen mode Exit fullscreen mode

If you execute the above code, you can get the following output:

Dummy product #1 - main.Product{name:"nexus robust appliance", description:"Even a I including retard point failure nothing therefore band after within even. Board there downstairs first suddenly arrive whole then everyone i.e.."}
Dummy product #2 - main.Product{name:"monitor fusion bronze", description:"Quiver fortnightly sufficient alternatively do enthusiastically before none whomever ours perfectly bear mock there. Myself their ever anything enough his light us. Instance e.g. has where i.e. nightly accordingly less tomorrow."}
Dummy product #3 - main.Product{name:"stainless purple router", description:"Care problem entertain ever for over infancy Alpine really where your I now generation bouquet. Election should whose elsewhere east easy between."}
Dummy product #4 - main.Product{name:"black suede microwave", description:"Whatever British your half eventually why guilt range health next in. All you exemplified are my joyous this it everyone what."}
Dummy product #5 - main.Product{name:"aqua vacuum spark", description:"Entirely meanwhile twist one consequently onto bright. There another park host their as without."}
Dummy product #6 - main.Product{name:"maroon lightbulb matrix", description:"Company me whichever city which up many tomorrow soak soon here some scream next lawyer. Of answer for moreover cautiously monthly over himself obnoxious now their you far."}
Dummy product #7 - main.Product{name:"core precision scale", description:"Problem confusion regularly whomever whom well you these sparse their might we funny. Stairs success your art in many regularly dive barely of anything where."}
Dummy product #8 - main.Product{name:"thermostat quick ultra-lightweight", description:"They conclude honestly then others woman just party. Since stand then her slide loneliness i.e. about now content alternatively. Under outside so his one whom it alternatively with."}
Dummy product #9 - main.Product{name:"swift wireless printer", description:"Become hourly many finally many we themselves what you circumstances."}
Dummy product #10 - main.Product{name:"compact mixer quick", description:"Just fun disappear sufficient due sometimes. French recently might there hence."}
Dummy product #11 - main.Product{name:"router core aluminum", description:"Knit cooperative whirl first these when unless its their will to why being. The fortnightly be unexpectedly this then respond walk consequence those arrogant today gossip."}
Dummy product #12 - main.Product{name:"portable microwave quick", description:"Gang firstly lot do person you muddy whose was life Intelligent had much. Under to back outfit coffee justice just do most that regularly am yesterday book yours."}
Dummy product #13 - main.Product{name:"energy-efficient headphones zen", description:"By left yell much cup outside."}
Dummy product #14 - main.Product{name:"fresh bronze router", description:"Herself it yearly those murder. When meanwhile alone while across Spanish each slavery her deliberately. What gossip other weather somebody annually that myself happily in."}
Dummy product #15 - main.Product{name:"maroon chrome car", description:"Those in in smiling whoever east annually thrill nobody my whom tomorrow factory where."}
Dummy product #16 - main.Product{name:"innovative user-friendly smartwatch", description:"Itself him many simply without somebody you next us host our for year include an. Not senator today are even any crew nearly today pipe reluctantly Portuguese."}
Dummy product #17 - main.Product{name:"pure tool turbo", description:"These without conclude somebody page party research have herself. E.g. where dynasty secondly covey egg that shake yesterday."}
Dummy product #18 - main.Product{name:"fuchsia felt tablet", description:"Only muster baby the in many. These tomorrow whose this its till Colombian patience foolishly. Dynasty your lots conclude whose each garlic pounce want did hers hers from cancel heavily."}
Dummy product #19 - main.Product{name:"modular water-resistant camera", description:"Myself should i.e. wad dig theirs."}
Dummy product #20 - main.Product{name:"router compact wireless", description:"For one ream theirs who mob party no abroad foot that before whom anything how."}
Enter fullscreen mode Exit fullscreen mode

Examples

In this section I will provide you some examples that may improve your understanding of this topic.

Product

In Product, you could find many functions including:

  • Name
  • Description
  • Category
  • Feature
  • Material

Implementation:

fakeProduct := gofakeit.Product()

fmt.Println("Product detail:")
fmt.Printf("Name: %v\n", fakeProduct.Name)
fmt.Printf("Description: %v\n", fakeProduct.Description)
fmt.Printf("Category: %v\n", fakeProduct.Categories)
fmt.Printf("Feature: %v\n", fakeProduct.Features)
fmt.Printf("Material: %v\n", fakeProduct.Material)
Enter fullscreen mode Exit fullscreen mode

Output:

Product detail:
Name: blue tablet edge
Description: Whose fun any them shower here elegantly board those.
Category: [educational toys sunglasses]
Feature: [touchscreen stylish smart biometric]
Material: stainless
Enter fullscreen mode Exit fullscreen mode

Person

In Person, you could find many properties including:

  • FirstName
  • LastName
  • Gender
  • SSN
  • Hobby
  • Email
  • Phone

Implementation:

fakePerson := gofakeit.Person()

fmt.Println("Person detail:")
fmt.Printf("First name: %v\n", fakePerson.FirstName)
fmt.Printf("Last name: %v\n", fakePerson.LastName)
fmt.Printf("Gender: %v\n", fakePerson.Gender)
fmt.Printf("SSN: %v\n", fakePerson.SSN)
fmt.Printf("Hobby: %v\n", fakePerson.Hobby)
fmt.Printf("Email: %v\n", fakePerson.Contact.Email)
fmt.Printf("Phone: %v\n", fakePerson.Contact.Phone)
Enter fullscreen mode Exit fullscreen mode

Output:

Person detail:
First name: Maci
Last name: Renner
Gender: male
SSN: 628070818
Hobby: Book collecting
Email: rachelroberts@ruecker.net
Phone: 5846012344
Enter fullscreen mode Exit fullscreen mode

Address

In Address, you could find many properties including:

  • City
  • Country
  • State
  • Street
  • Zip
  • Latitude
  • Longitude

Implementation:

fakeAddress := gofakeit.Address()

fmt.Println("Address detail:")
fmt.Printf("City: %v\n", fakeAddress.City)
fmt.Printf("Country: %v\n", fakeAddress.Country)
fmt.Printf("State: %v\n", fakeAddress.State)
fmt.Printf("Street: %v\n", fakeAddress.Street)
fmt.Printf("Zip: %v\n", fakeAddress.Zip)
fmt.Printf("Latitude: %v\n", fakeAddress.Latitude)
fmt.Printf("Longitude: %v\n", fakeAddress.Longitude)
Enter fullscreen mode Exit fullscreen mode

Output:

Address detail:
City: Baltimore
Country: Niue
State: Virginia
Street: 7441 Port Throughwayhaven
Zip: 28791
Latitude: 54.08894
Longitude: -69.041932
Enter fullscreen mode Exit fullscreen mode

There you have it! I hope you find this blog post is helpful and can improve your productivity. Thank you for reading, and have a nice day!

Source:

Cover image:

Photo by Suzy Hazelwood from Pexels: https://www.pexels.com/photo/scrabble-board-game-on-shallow-focus-lens-1153929/

Top comments (2)

Collapse
 
brianvoe profile image
Brian Voelker

Thanks for using my package! I hope you found it helpful.

Collapse
 
luthfisauqi17 profile image
luthfisauqi17

Thank you for creating this package, it's super helpful 😁