Skip to content

Instantly share code, notes, and snippets.

@maguec
Created August 26, 2023 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maguec/7a18090f9a390c2be47aad7d382ac960 to your computer and use it in GitHub Desktop.
Save maguec/7a18090f9a390c2be47aad7d382ac960 to your computer and use it in GitHub Desktop.
GoFakeIt to JSON file example
package main
import (
"encoding/json"
"os"
"github.com/brianvoe/gofakeit/v6"
)
type Record struct {
Uuid string `fake:"{uuid}" json:"uuid"`
UserId string `fake:"{username}" json:"username"`
FirstName string `fake:"{firstname}" json:"firstname"`
LastName string `fake:"{lastname}" json:"lastname"`
Company string `fake:"{company}" json:"company"`
Title string `fake:"{jobtitle}" json:"title"`
Email string `fake:"{email}" json:"email"`
Cell string `fake:"{phoneformatted}" json:"cellphone"`
Years int `fake:"{number:1,10}" json:"tenure"`
}
func main() {
var r Record
f, err := os.Create("data.json")
if err != nil {
panic(err)
}
for i := 1; i <= 1000000; i++ {
gofakeit.Struct(&r)
data, _ := json.Marshal(r)
_, err := f.WriteString(string(data) + "\n")
if err != nil {
panic(err)
}
}
err = f.Close()
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment