DEV Community

Cover image for Code Your First Rest API in Golang
Gaurish Sethia
Gaurish Sethia

Posted on

1

Code Your First Rest API in Golang

#go

This tutorial would explain you about how you're going to code a basic rest api in golang,
We'd be using gin a http web server framework.

Let's begin.

Getting Started

Open up your shiny Terminal,
Run the Commands

go mod init gin-tutorial
touch main.go
go get -u github.com/gin-gonic/gin
Enter fullscreen mode Exit fullscreen mode

Terminal

Now, Let's Navigate to our main.go and start writing code with vscode

On top of the file, Describe the package

package main
Enter fullscreen mode Exit fullscreen mode

Now, We'd be importing gin,

package main

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

Make sure you install gin via the command go get -u github.com/gin-gonic/gin

Now, We'll start making our router,

package main

import "github.com/gin-gonic/gin"

func main() {
    r := gin.Default()
    r.SetTrustedProxies([]string{"192.168.1.2"})
    r.Run()
}
Enter fullscreen mode Exit fullscreen mode

Read more about setTrustedProxies here

So, If we run go run main.go, This is what we're gonna get...

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080

Enter fullscreen mode Exit fullscreen mode

Now, Open up localhost:8080 on your browser, You'd see a 404 page not found response, Yes, Because we haven't created any routes yet.

Creating Routes

Get Requests

  • Get requests are handled as .GET, Same applies for all types of requests, Just replace GET with the type of Request,

Let's create a test route:

r.GET("/get", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "status": "healthy",
        })
    })
Enter fullscreen mode Exit fullscreen mode

So, If you now navigate to localhost:8080/get, You see a JSON Response

{"status":"healthy"}
Enter fullscreen mode Exit fullscreen mode

Perfect!

Congratulations, You Just Learned to Create Your First Rest API With Golang.

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (0)

PulumiUP 2025 image

PulumiUP 2025: Cloud Innovation Starts Here

Get inspired by experts at PulumiUP. Discover the latest in platform engineering, IaC, and DevOps. Keynote, demos, panel, and Q&A with Pulumi engineers.

Register Now

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay