Golang MySQL, GORM, Echo

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

Image for post

We are using the following framework and libraries for this article

  1. Echo Framework (Highly Scalable API development framework)
  2. 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

Image for post

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

Image for post
Image for post

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
Image for post

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

Image for post

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

Image for post

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

  1. A
  2. B
  3. 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
|---
01.sql (File)
|-- config
|--- db.go
Image for post

Create a student.go, Model

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

GoLangMySqlHelloWorld
|-- config
|-- db.go
|-- model
|-- student.go
Image for post

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
Image for post

above file, we have two different method

  1. NewDB: To initialize database connection
  2. 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
Image for post

Above file, we have created 2 methods

  1. GetStudents: to use as a route to display the student’s records as a JSON
  2. 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
Image for post

Above file, you will see we have two Routes

  1. e.GET(“/”, hello): will print Hello, world
  2. 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

Image for post

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

http://localhost:1323/students
Image for post

Written by

We teach & develop for startups

Get the Medium app