Golang CRUD Rest API with Echo

Golang CRUD Rest API with Echo

In this Golang tutorial, we’ll guide you through building a CRUD (Create, Read, Update, Delete) operation API using the PostgreSQL database. Our aim is to demonstrate how to add, update, and delete records from a PostgreSQL table by constructing a REST API.

Using Echo Framework

Utilizing the ECHO web framework is both basic and straightforward. With the Echo framework, we’ll create a ‘todo’ application for managing employee records in the database. This application enables the creation, editing, and deletion of employee records.

What is Echo Framework?

Echo is a PHP micro-framework similar to Lumen, designed for developing quick and secure web applications with Go. While you can design a RESTful API without a framework, using frameworks provides additional features enhancing security and speed in web development.

PostgreSQL Database Table

Let’s start by creating a table in the PostgreSQL database. The ’employees’ table will include the following columns:

  • name
  • salary
  • age

REST Endpoints

We’ll define the following REST endpoints in this Golang REST API tutorial:

RouteMethodTypePosted JSONDescription
/employeeGETJSONGet all employees’ data
/employee/{id}GETJSONGet a single employee’s data
/employeePOSTJSON{“Name”: “Adam”, “Salary”: “4500”, “Age”: “53”}Insert a new employee record
/employee/{id}PUTJSON{“Name”: “Adam”, “Salary”: “4500”, “Age”: “53”, “Id”: “16”}Update a specific employee record
/employee/{id}DELETEJSON{“Id”: 16}Delete a particular employee record

Creating the Rest API Framework

To build our RESTful API, we’ll use the Go programming language, a lightweight and efficient language developed by Google. Additionally, we’ll employ the Echo framework and store information in a PostgreSQL database.

Step 1: Create main.go file

package main

import (
    "net/http"
    "database/sql"
    "github.com/labstack/echo"
    "fmt"
    "log"
    _ "github.com/lib/pq"
)

Here, we import necessary packages and initialize the PostgreSQL database connection.

Step 2: Install packages

$ go get github.com/labstack/echo
$ go get github.com/lib/pq

Step 3: Implement main() function

var err error
db, err = sql.Open("postgres", "user=postgres password=root dbname=books_database sslmode=disable")
if err != nil {
    log.Fatal(err)
}
if err = db.Ping(); err != nil {
    panic(err)
} else {
    fmt.Println("DB Connected...")
}
e := echo.New()
// Employee and Employees structs are defined here...

In this code snippet, we set up the database connection and initialize the Echo framework.

The rest of the code includes the implementation of various REST API endpoints for handling CRUD operations.

Get All Records Using Rest API

e.GET("/employee", func(c echo.Context) error {
    // Implementation for fetching all records...
})

Add a Record Using API

e.POST("/employee", func(c echo.Context) error {
    // Implementation for adding a record...
})

Update Record Using Rest API

e.PUT("/employee", func(c echo.Context) error {
    // Implementation for updating a record...
})

Delete a Record Using Rest API

e.DELETE("/employee/:id", func(c echo.Context) error {
    // Implementation for deleting a record...
})

Final Thought

In this tutorial, we used the Golang Echo framework and the PostgreSQL database to create a RESTful API. This includes adding records, updating data, retrieving all data, and deleting data through RESTful API calls. The tutorial focuses on the fundamentals of routing and utilizing the Echo framework in Golang.

You May Also Like: