GoLang — Introduction | Create REST API with Auto Reload

gyanendraknojiya

Gyanendra Kumar Knojiya

Posted on December 3, 2021

GoLang — Introduction | Create REST API with Auto Reload

GoLang — Introduction | Create REST API with Auto Reload

Go is a High-Level, Strong ( you can not change variables type ) and Statically (All variables should be declared at compile time ) typed, complied programing language created by Google.

Key Features

  • Simplicity — Go have only very useful and necessary features.
  • Fast Compile — Go have very little compile time.
  • Garbage Collection — Garbage collection is a type of memory management that is automated.
  • Built-In-Concurrency — The capacity for functions to operate independently of one another.
  • Compiled into standalone binaries

Gin — Web Development Framework

Gin allows you to create Go web apps and microservices. It includes a set of frequently used capabilities (e.g., routing, middleware support, rendering, etc.)

In this article, I am going to create a simple GO server using Gin that will allow you to perform CRUD (Create, Read, Update, Delete) operations. I will also implement auto-reload so you do not need to restart the server again and again after making some changes.

Create a server using Gin

Steps are included in this process-

  1. Create a GO project.
  2. Install and import Gin in the project.
  3. Add AIR config for auto-reload.
  4. Add get, post, put, and update APIs.
  5. Validate responses using postman.

So, we are going to start with the very first step-

Create a GO project

First, we need to initialize GO in the project.

go mod init gin-example-project
Enter fullscreen mode Exit fullscreen mode

It will create a go.mod file in the root folder of ours project.

Now, we can create main.go file to start writing code.

To test everything working, paste hello word program in main.go file and run it.

package main
import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
Enter fullscreen mode Exit fullscreen mode

To run this code, simply command-

go run main.go

_// expected output: Hello, World!_
Enter fullscreen mode Exit fullscreen mode

Install and Import Gin in Project

To install Gin, Go v1.13+ is required. To install gin, run-

go get -u github.com/gin-gonic/gin
Enter fullscreen mode Exit fullscreen mode

After installing, you can import gin in your main.go file.

import "github.com/gin-gonic/gin"
Enter fullscreen mode Exit fullscreen mode

We also need net/http to send status.

import "net/http"
Enter fullscreen mode Exit fullscreen mode

Add AIR config for auto reload

Create a .air.conf file in the root folder of the project and add this config in that file.

import _ "github.com/joho/godotenv/autoload"
root = "."
tmp_dir = "tmp"

[build]
_# Just plain old shell command. You could use `make` as well._
cmd = "go build -o ./tmp/main ."
_# Binary file yields from `cmd`._
bin = "tmp/main"
_# Customize binary._
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
_# Watch these filename extensions._
include_ext = ["go", "tpl", "tmpl", "html"]
_# Ignore these filename extensions or directories._
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
_# Watch these directories if you specified._
include_dir = []
_# Exclude files._
exclude_file = []
_# It's not necessary to trigger build each time file changes if it's too frequent._
delay = 1000 _# ms_
_# Stop to run old binary when build errors occur._
stop_on_error = true
_# This log file places in your tmp\_dir._
log = "air_errors.log"

[log]
_# Show log time_
time = false

[color]
_# Customize each part's color. If no color found, use the raw app log._
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"

[misc]
_# Delete tmp directory on exit_
clean_on_exit = true
Enter fullscreen mode Exit fullscreen mode

Now, you have to run your project using AIR. So you need to command-

air run main.go
Enter fullscreen mode Exit fullscreen mode

Add GET, POST, PUT, and UPDATE APIs

I have added some sample data for this. You can copy it from here https://github.com/gyanendraknojiya/GO-Gin-REST-API#get-all-users

Create a main function in main.go file, and run your server a port 8080 (Port can be any).

func main(){
    router := gin.Default()
    router.Run("localhost:8080")
}
Enter fullscreen mode Exit fullscreen mode

Now you can see the server running. We need to create user json.

type user struct {
    ID string `json:"id"`
    Name string `json:"name"`
    UserName string `json:"userName"`
    Email string `json:"email"`
    Phone string `json:"phone"`
    Website string `json:"website"`
}

var users = []user{
    {
        ID: "1",
        Name: "Leanne Graham",
        UserName: "Bret",
        Email: "Sincere@april.biz",
        Phone: "1-770-736-8031 x56442",
        Website: "hildegard.org",
    },
    {
        ID: "2",
        Name: "Ervin Howell",
        UserName: "Antonette",
        Email: "Shanna@melissa.tv",
        Phone: "010-692-6593 x09125",
        Website: "anastasia.net",
    },
}
Enter fullscreen mode Exit fullscreen mode

Add all APIs in main function-

func main() {
    router := gin.Default()

    router.GET("/", getAllUsers)
    router.GET("/user/:id", getUserById)
    router.POST("/addUser", addUser)
    router.PUT("/updateUser", updateUser)
    router.DELETE("/deleteUser", deleteUser)

    router.Run("localhost:8080")
}
Enter fullscreen mode Exit fullscreen mode

getAllUsers

func getAllUsers(c *gin.Context) {
    c.IndentedJSON(http.StatusOK, users)
}
Enter fullscreen mode Exit fullscreen mode

getUserById

func getUserById(c *gin.Context) {
    ID := c.Param("id")
    for _, i := range users {
        if i.ID == ID {
            c.IndentedJSON(http.StatusOK, i)
            return
        }
    }
    c.IndentedJSON(http.StatusNotFound, gin.H{"message": "user not found!"})
}
Enter fullscreen mode Exit fullscreen mode

addUser

func addUser(c *gin.Context) {
    var newUser user

    if err := c.BindJSON(&newUser); err != nil {
        fmt.Println(err)
        return
    }

    users = append(users, newUser)
    c.IndentedJSON(http.StatusOK, users)
}
Enter fullscreen mode Exit fullscreen mode

updateUser

func updateUser(c *gin.Context) {
    var updateUser user

    if err := c.BindJSON(&updateUser); err != nil {
        fmt.Println(err)
        return
    }
    if updateUser.ID == "" {
        c.IndentedJSON(400, gin.H{"message": "id is missing!"})
        return
    }

    for i, user := range users {
        if user.ID == updateUser.ID {

            if updateUser.Name != "" {
                user.Name = updateUser.Name
            }
            if updateUser.Email != "" {
                user.Email = updateUser.Email
            }
            if updateUser.Phone != "" {
                user.Phone = updateUser.Phone
            }
            if updateUser.Website != "" {
                user.Website = updateUser.Website
            }
            users[i] = user
            c.IndentedJSON(http.StatusOK, users)
            return
        }
    }
    c.IndentedJSON(400, gin.H{"message": "id is not found!"})
}
Enter fullscreen mode Exit fullscreen mode

deleteUser

func RemoveIndex(s []user, index int) []user {
    return append(s[:index], s[index+1:]...)
}

func deleteUser(c *gin.Context) {
    var deleteUser user

    if err := c.BindJSON(&deleteUser); err != nil {
        fmt.Println(err)
        return
    }
    if deleteUser.ID == "" {
        c.IndentedJSON(400, gin.H{"message": "id is missing!"})
        return
    }

    for i, user := range users {
        if user.ID == deleteUser.ID {
            users = RemoveIndex(users, i)
            c.IndentedJSON(http.StatusOK, users)
            return
        }
    }
    c.IndentedJSON(400, gin.H{"message": "id is not found!"})
}
Enter fullscreen mode Exit fullscreen mode

That’s all. We can now utilize these APIs to get, add, update, and remove data from the server.

Source code: https://github.com/gyanendraknojiya/GO-Gin-REST-API

Buy me a coffee here https://www.buymeacoffee.com/gyanknojiya

If you have any queries, feel free to contact me here

Originally published at https://codingcafe.co.in on December 2, 2021.

💖 💪 🙅 🚩
gyanendraknojiya
Gyanendra Kumar Knojiya

Posted on December 3, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related