Lets Build A GO API

By | 10th October 2017

I started learning a bit of GoLang in my spare time, and the only way you can fully understand what you are learning in programming is by building something, even if it’s just a simple project.
I always been intrested in API’s so I decided to build a goLang API to learn more about Go and understand more about building your own API.

If anyone wants to learn golang a very good place to start is go’s own tutoial which can be found here.

So what are we going to build?
We are going to build a api that allows us to add products to a database, get a single product, get all products and update products.

What is a API?
To put it in a very simple way a API lets the program you write to control or access a program somebody else wrote e.g reddit api allows you to access your inbox messages to read them, mark as read, send a message etc.

Createing The Database
Firstly we will create a database for this project. We could just use some SQL code to create the database, but we may as well use a migration file to create the table for us.
first login to your mysql console and type the following

now we will create the migration file which will create the table product in the database.

Lets get into the coding
Now open up whatever editor you use(mine is sublime). Also what is handy is to install GoSublime sublime package.

We will start with the imports we need for our project.

create a project folder and within that folder create a file called go-api. Now open the file created and add the below imports to get started.

Here’s a little bit on what these imports are used for.
databse/sql – needed for the interface around SQL database, used with mysql driver.
net/http – Http client and server implementations.
os – Package os provides a platform-independent interface to the operating system functionality.
go-sql-driver – Mysql driver.
gin-gonic – implements a HTTP web framework.

For this tutorial I am using Heroku and the first line of code in the snipptet below is to get the port that heroku has set. Next we set the user, password, ip, database name we are using which will return a panic message if it can’t connect to the database. we use db.Ping() to check if the database is available.

We are creating a struct called Product, this struct will be used to get data when getting records from DB. We should also set the default for gin.

The below gin route will let us get details of a certain product. It will take the id from the url and check the database for that id and return the description, price of that prodcut in JSON format with 201 status, if there is no product found it will return no product found message.

The below route will get all products that are in the database and return the description , price for each product, if the databse is empty it will return “no product found message”.

This route will allow us to delete a product from the database if sucessful it will return a 201 status with a message “product deleted successfuly”.

This route will allow us to add a new product to the databse. It takes two parameters, description and a price and enters it into the database, if successful it will return a JSON message saying “product created sucessfully” and with the id of the product created.

And finally the last part of our code is used to run our application and the port it is listening on.

Trying Out The API

Now we are going to try out each part of our api. To do this we are going to use postman.

GET Single Product
below you can see i entered the id of the product I wanted and it returned the description and price of that product.

GET All Products
This will get us all the products in the product table.

Delete a product
To delete a product, we use a delete request with the id of the product we need deleted as can be seen in the image below.

Add new product to the database
We are going to try and add a new product to the database, we need two fields in form data, p_desc, price with what values we need, after pressing the send button the product was successfully added to the database.

Update a product
Lets try and update a product in the database.We use the id of the product we need to update, change to put request and add the updated data to the form data under p_desc and price

That’s it for this post, you can view the source code for this here.