This article will discuss about one of from backend famework in Go Language. The framework is Gin Gonic Framework. Gin Gonic is the framework to build API. For more detail, What is the gin gonic framework? Gin is a web framework written in Go (Golang). It features a martini-like API with much better performance, up to 40 times faster thanks to httprouter. If you need performance and good productivity, you will love Gin. For Detail Description you can see here. Gin Gonic

CURD Application need API’s like belows:

  1. POST todos/
  2. GET todos/
  3. GET todos/{id}
  4. PUT todos/{id}
  5. DELETE todos/{id}

This example will use Mysql database for the connection the services. and we will setup connection when starting write code.

The first need Go installed (version 1.10+ is required), then you can use the below Go command to install Gin.

Go to your $GOPATH/src and make a directory gin-login. Inside the gin-login directory create 4 folder like this controllers,models,mappings,main. Example structure folder :


Inside the main directory create a file main.go.Import the “gin framework” to our project and create the routes like below inside main function.

I like to add a prefix of the api’s like “v1/users/”, that’s why we’ll use the router Group in mappings directory method like below.

We have created five routes and they handle some functions like GetUserDetail,  GetUser, , etc.

Now we need to setup a database connection. To use database pull the gorm package and mysql dialects in our code in folder controllers. Follow the code below:

In the above code “mysql” is our database driver, “user” is database username, “password” password and “test” is database name. Please change these information as your needs.

We’ll use the Database function to get the database connection. Lets make a User struct in directory Models. The first struct will represent the original User and the second one will hold the transformed user for response to the api. Here we transformed the todo response because we don’t expose some database fields (updated_at, created_at) to the consumer.

User struct has one field extra gin-gonic.Model what does it mean? well, this field will embed a Model struct for us which contains four fields “ID, CreatedAt, UpdatedAt, DeletedAt”.

Later implement the functions in directory controllers with name login.go

Well, we write enough code, let try to build the app and test it, I’m going test it using chrome extension Postman (you can use any REST client like curl to test).

To build the app open your terminal and go the the main directory

The command will build a binary file main and to run the file us this command $ .main/main.go . Wow, our example project app is running on port: 8080. It’ll display the debug log, because by default gin run’s in debug mode and port 8080.

To test the api run postman and test the api sequentiall

Fetch All Users


Fetch Single User


Post a User


Login User


You can see Gin Gonic Mysql Golang Example in Here.

Thank you for reading this article about Gin Gonic Mysql Golang Example, I hope this article is useful for you. Visit My Github about Go in Here

Gin Gonic Mysql Golang Example
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *