Skip to content

Instantly share code, notes, and snippets.

@tvhung83
Last active December 6, 2018 08:26
Show Gist options
  • Save tvhung83/b685c0cf13f022a88aae7b840d2acd88 to your computer and use it in GitHub Desktop.
Save tvhung83/b685c0cf13f022a88aae7b840d2acd88 to your computer and use it in GitHub Desktop.
Generate random data to CSV file
package main
import (
"os"
"strconv"
"github.com/gocarina/gocsv"
"github.com/bxcodec/faker"
)
type Record struct {
Latitude float32 `faker:"lat"`
Longitude float32 `faker:"long"`
CreditCardNumber string `faker:"cc_number"`
CreditCardType string `faker:"cc_type"`
Email string `faker:"email"`
IPV4 string `faker:"ipv4"`
IPV6 string `faker:"ipv6"`
Password string `faker:"password"`
PhoneNumber string `faker:"phone_number"`
MacAddress string `faker:"mac_address"`
URL string `faker:"url"`
UserName string `faker:"username"`
ToolFreeNumber string `faker:"tool_free_number"`
E164PhoneNumber string `faker:"e_164_phone_number"`
TitleMale string `faker:"title_male"`
TitleFemale string `faker:"title_female"`
FirstName string `faker:"first_name"`
FirstNameMale string `faker:"first_name_male"`
FirstNameFemale string `faker:"first_name_female"`
LastName string `faker:"last_name"`
Name string `faker:"name"`
UnixTime int64 `faker:"unix_time"`
Date string `faker:"date"`
Time string `faker:"time"`
MonthName string `faker:"month_name"`
Year string `faker:"year"`
DayOfWeek string `faker:"day_of_week"`
DayOfMonth string `faker:"day_of_month"`
Timestamp string `faker:"timestamp"`
Century string `faker:"century"`
TimeZone string `faker:"timezone"`
TimePeriod string `faker:"time_period"`
Word string `faker:"word"`
Sentence string `faker:"sentence"`
Paragraph string `faker:"paragraph"`
Currency string `faker:"currency"`
Amount float64 `faker:"amount"`
AmountWithCurrency string `faker:"amount_with_currency"`
UUIDHypenated string `faker:"uuid_hyphenated"`
UUID string `faker:"uuid_digit"`
}
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
file, err := os.OpenFile(os.Args[1], os.O_RDWR|os.O_CREATE, os.ModePerm)
check(err)
defer file.Close()
num, err := strconv.ParseInt(os.Args[2], 10, 32)
check(err)
rows := []*Record{}
for i := 0; i < int(num); i++ {
a := Record{}
err = faker.FakeData(&a)
check(err)
rows = append(rows, &a)
}
gocsv.MarshalFile(&rows, file)
}
@tvhung83
Copy link
Author

tvhung83 commented Dec 6, 2018

$ go get -u github.com/bxcodec/faker
$ go get -u github.com/gocarina/gocsv
$ go run generate.go <file.csv> <number_of_records>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment