Skip to content

This repository is just me learning how to set up a basic server in Gin Framework in Golang.

Notifications You must be signed in to change notification settings

rabi-siddique/backend-golang-gin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Why Use Gin in Go?

Gin is a popular web framework in Go, known for its simplicity, speed, and flexibility. It is often chosen for building web applications and APIs due to its lightweight nature and built-in features. Here are some key reasons why developers choose Gin for Go:

  • Ease of Use: It offers a simple API that allows developers to get started quickly with clean and readable code.
  • Rich Feature Set: Gin includes middleware support, routing JSON validation, error handling, and more out of the box.
  • Good Documentation: The framework is well-documented and has a large community, making it easier to find solutions to common problems.
  • Built-in JSON Validation: Gin can bind incoming requests to Go structs, which is useful when working with JSON APIs.

Basic Gin Application in Go

This is a simple example of a basic Gin server with one route:

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	// Create a Gin router
	router := gin.Default()

	// Define a simple GET endpoint
	router.GET("/hello", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "Hello, World!",
		})
	})

	// Start the Gin server on port 8080
	router.Run(":8080")
}

Features in the Example:

  • Routing: We define a simple route GET /hello using router.GET().
  • JSON Response: The route handler sends a JSON response using c.JSON().
  • Server Startup: The server is started on port 8080 using router.Run(":8080").

JSON Binding and Validation

Gin supports binding of incoming JSON requests into Go structs. You can easily bind the request body to a struct and validate it. Here's an example of how to do it:

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type User struct {
	Name  string `json:"name" binding:"required"`
	Email string `json:"email" binding:"required"`
}

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

	// POST route to bind JSON data
	r.POST("/user", func(c *gin.Context) {
		var user User
		if err := c.ShouldBindJSON(&user); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		c.JSON(http.StatusOK, gin.H{"message": "User data received", "data": user})
	})

	r.Run(":8080")
}

In this example:

  • The binding tag ensures that the Name and Email fields are required.
  • The ShouldBindJSON function binds the incoming JSON data to the User struct.

URL Parsing

Gin makes it easy to parse parameters from the URL. You can access route parameters with c.Param():

r.GET("/user/:id", func(c *gin.Context) {
	id := c.Param("id")
	c.JSON(http.StatusOK, gin.H{"user_id": id})
})

In this example, if the request URL is /user/123, the id parameter will be extracted and sent as part of the response.

Why Choose Gin Over the Standard net/http?

Feature Gin Framework Go net/http
Performance Optimized for performance Basic, not as optimized as Gin
Routing Advanced routing capabilities Basic routing with http.Handle
Middleware Support Built-in middleware support Requires custom implementation
JSON Handling Can handle JSON binding and validation Requires manual parsing of JSON
Error Handling Built-in error handling features Basic error handling
Community Support Large and active community Smaller community with fewer tools

About

This repository is just me learning how to set up a basic server in Gin Framework in Golang.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published