Golang MySQL
Tags: MySQL, Golang, Onexlab, Golang Mysql, Gorm

In this article, We will show you how to connect to the MySql Database using Golang.

We are using the following framework and libraries for this article

Echo Framework (Highly Scalable API development framework) GORM (GoLang Object Relation Mapping/Database Connectivity)

Video Tutorial

Golang Configuration

In our case, we have configured the Golang path in the .bash_profile file using MacOS as following

export GOPATH=$HOME/go

export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

In the go directory, we have a folder called github.com

Inside the github.com directory, we have our GitHub user directory called oxlb as showing the following screenshot

We will create a new GoLang project under the oxlb directory

Create a Golang Project

Go to your go configuration directory and create a new directory called

GoLangMySqlHelloWorld

Open the terminal and change the directory to GoLangMySqlHelloWorld

$ cd GoLangMySqlHelloWorld

Then create a go.mod file using the following file

$ go mod init github.com/oxlb/GoLangMySqlHelloWorld

this will create the go.mod file as shown below

The above file contains the module and version of the GoLang

Docker Compose MySql And Adminer

We are using docker-compose to use MySql and Adminer for the database connectivity test.

If you need more help to understand you can see our previous article for Docker Compose MySql

Create SQL Script File 01.sql under init Directory

Create docker-compose.yml file

above file, we have created MySql and Adminer Docker Services

if you see the docker-compose.yml file line no. 17–19 there is a volumes script there as shown below

The above script will create a students table during docker startup and insert 3 records in the table

A B C

Run Docker Container

Make sure you have installed Docker on your Machine/System

We have created the video tutorial for Docker Installation below

Try to run the following command to run the container

docker-compose up

Add Echo Framework Dependency using the following command

go get github.com/labstack/echo/v4

Add GORM Dependency using the following command

go get -u github.com/jinzhu/gorm

Now you will see your go.mod file looks like the below file

Create db.go

Create new file under config directory /config/db.go

GoLangMySqlHelloWorld

|-- docker-compose.yml (File)

|-- init

|--- (File)

|-- config

|--- db.go |-- docker-compose.yml (File)|-- 01.sql (File)|--|--- db.go

Create a student.go, Model

Create new file under model directory /model/student.go

GoLangMySqlHelloWorld

|-- config

|-- db.go

|-- model

|-- student.go

From the above model, we will fetch Id and Name from the database table students

Create a db.go

Create new file under storage directory /storage/db.go

GoLangMySqlHelloWorld

|-- storage

|-- db.go

above file, we have two different method

NewDB: To initialize database connection GetDBInstance: to get the DB instance

Create a student.go

Create new file under controller directory /controller/student.go

GoLangMySqlHelloWorld

|-- controller

|-- student.go

|-- storage

|-- db.go

Above file, we have created 2 methods

GetStudents: to use as a route to display the student’s records as a JSON GetRepoStudents: to fetch the records from the table students

Create a main.go

Create a new file under GoLangMySqlHelloWorld directory /GoLangMySqlHelloWorld/main.go

GoLangMySqlHelloWorld

|-- config

|-- db.go

|-- controller

|-- student.go

|-- model

|-- student.go

|-- storage

|-- db.go

|-- main.go

Above file, you will see we have two Routes

e.GET(“/”, hello): will print Hello, world e.GET(“/students”, controller.GetStudents): will print

Run Main.go

To run the main file type the following command

go run main.go

you will see it will show you server is running on port:1323

If you try to open the following URL in the Web browser