All Things Computers
2 min readMar 21, 2023

Technical Interview Challenge: Using Gorm with MySQL

Recently, I was given a technical interview assignment that pushed me to try new things in my Golang development skills. One of the tasks was to use Gorm to connect to a MySQL database and print the contents of the database. In this blog post, I will share with you the steps I took to achieve this task.

Step 1: Set up the environment
To get started, you need to have the following installed on your machine:

Visual Studio Code
Golang
MySQL
Gorm

Step 2: Create a new Golang project
Open Visual Studio Code and create a new Golang project. You can do this by opening the terminal and running the following command:

mkdir myproject
cd myproject
go mod init myproject

Step 3: Install Gorm and the MySQL driver
Next, you need to install Gorm and the MySQL driver. Run the following commands in the terminal:

go get -u gorm.io/gorm
go get -u gorm.io/driver/mysql
Step 4: Set up the database connection
Now it's time to set up the database connection. Create a new file called database.go in the root of your project and add the following code:

package main

import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

func ConnectDB() (*gorm.DB, error) {
dsn := "user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
}
return db, nil
}
Replace user, password, and dbname with your MySQL credentials and database name. This function will return a *gorm.DB object that we can use to interact with the database.

Step 5: Create a model and a migration
To print the contents of the database, we need to create a model and a migration. Create a new file called model.go in the root of your project and add the following code:

package main

import (
"gorm.io/gorm"
)

type User struct {
gorm.Model
Name string
Email string
}

func Migrate() error {
db, err := ConnectDB()
if err != nil {
return err
}
db.AutoMigrate(&User{})
return nil
}
This will create a User model and a migration that will create a users table in the database.

Step 6: Insert some data
Before we can print the contents of the database, we need to insert some data. Create a new file called main.go in the root of your project and add the following code:

package main

import (
"fmt"
)

func main() {
err := Migrate()
if err != nil {
panic(err)
}

db, err := ConnectDB()
if err != nil {
panic(err)
}
defer db.Close()

user := User{Name: "John", Email: "john@example.com"}
db.Create(&user)

var users []User
db.Find(&users)

fmt.Println(users)
}
This code will create a new User object, insert it into the database, fetch all the User objects from the database, and print them to the console.

Step 7: Run the code
Now it's time to run the code. Open the terminal in Visual Studio Code and run

All Things Computers
All Things Computers

Written by All Things Computers

Providing support for software development, data science, AI/Machine Learning, CyberSecurity, threat hunting and hacking.

No responses yet