Generating fake data with Golang.
When we are building applications, we often need some data to be queried, manipulated, displayed and so on.
While we are focused on writing code, we don't want to worry about on how to populate the database with some data.
There are tools that can generate some data for us, saving our development time and preveting on using real names, ids, dates.
Today we are going to see an example using bxcodec/faker.
Application
Let’s start creating new webserver using Fiber Web Framework.
Fiber
Fiber is Express inspired web framework written in Go.
On the terminal, create a new directory to write application code.
$ mkdir fakedataapp
$ cd fakedataapp
Download Fiber package with the command:
go get -u github.com/gofiber/fiber
Create a new file called main.go, the service will be exposed on port 8000, you can change the port number if you wish.
$ vim main.go
This is the initial code:
Struct
On the same file, we declare our struct with the fields to be populated.
Creating the Database
In this example we are using MySQL Database to store the data. Feel free to use your favourite tool to connect on your database, in the example shown right below, the commands were set by using terminal.
We supposed your MySQL Database are already installed and configured.
Mysql
- -u: user.
- -p: password will be prompted.
$ mysql -u root -p
Command line to create the database:
$ mysql> CREATE DATABASE gomysqlfakedata;
Connecting to the database
Now we are going to write the code to connect our application to the database using framework Gorm.
Gorm
The fantastic ORM library for Golang aims to be developer friendly.
On terminal, remember to download de library:
$ go get -u gorm.io/gorm
Gorm supports MySQL, SQLite, Postgres, SQLServer.
DSN will be user:password@/databasename:
"root:root@/gomysqlfakedata"
Auto Migrate
You can easily create the table in the database using Auto Migrate. You just need to pass the Item struct as argument on db.AutoMigrate(&Item{}).
Run the application to create the table on the database.
$ go run main.go
Using mysql terminal to check if the table was successfully created
USE gomysqlfakedata;
SHOW TABLES;
Populating
Let's create the function to populate the table. Before writting the code, download the faker library:
go get -u github.com/bxcodec/faker
POST Endpoint
Create a new POST endpoint "/api/item/create", passing fiber function as parameter and create a 100 loop interactions, this is the total items to be created on the table.
We set the fields Name, Description and Price to be automatically populated.
- faker.Word(): generate a random fake word.
- faker.Paragraph(): generate a random fullfake paragraph.
- Price: set by using a random number between 10 and 150.
We return a JSON on fiber message with "success" and status code 200.
Retrieving
This endpoint will query database to retrieve all the fake data storaged on it and the will return an JSON array
GET Endpoint
Executing
Lets run the application:
$ go run main.go
Using Curl, Postman or any preferred tool to call the API using POST method on endpoint: "http://localhost:8000/api/item/create"
$ curl -X POST localhost:8000/api/item/create
Checking
Using curl or browser to retrieve the data generated:
$ curl localhost:8000/api/item/all
Browser
http://localhost:8000/api/item/all
Full Code
The source code can also can be download on github:
Thanks for reading.
References:
https://gorm.io/index.html
https://github.com/gofiber/fiber
https://github.com/bxcodec/faker