Why
I think it’s a good idea to start with a Gin a web framework for Golang to understand advanced usage of Golang. So I decided to learn Gin day by day. (it’s a unstable plan XD)
What is Gin
Let’s see how Copilot explain Gin.
Gin is a web framework written in Go (Golang). It’s known for its lightweight nature, high performance, and low memory footprint. Here are the main features of Gin:
- Speed: Gin uses a custom version of HttpRouter, which makes it extremely fast.
- Middleware support: Gin allows the use of middleware in the request processing pipeline, making it easy to implement features like logging and error handling.
- Error management: Gin provides a convenient mechanism for error handling.
- JSON validation: It has built-in JSON validation functionality, making it easy to handle JSON data.
- Route grouping: Gin supports grouping routes, which is useful for managing APIs in large applications.
- Rendering: It supports rendering XML, JSON, and HTML.
Gin is excellent for building API services and can also be used for developing full-stack web applications. Its design philosophy is to keep the core simple while allowing functionality to be added through middleware and extensions. For Go web developers who need high performance and flexibility, Gin is an excellent choice.
Project Setup
-
Create a new directory for your project and navigate into it.
-
Initialize a new Go module.
go mod init hello-gin
-
Install Gin.
go get -u github.com/gin-gonic/gin
-
Create a main.go file.
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/hello-world", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "Hello World",
})
})
r.Run()
} -
Use Terminal to Run the application.
go run main.go
Explanation code
import
we import gin package from Gin. maybe can refer this repository Gin.
func main()
This is the entry point of the application.
r := gin.Default()
This creates a new Gin router with default settings.
r.GET("/", func(c *gin.Context) { ... })
This sets up a route for GET requests at the root URL ("/"). When a GET request is made to this URL, the function passed as the second argument is executed. It sends a JSON response with a status code of 200 and a message “Hello World”.
r.Run()
This starts the HTTP server and listens for incoming requests on port 8080 by default.
we can change the port by passing a parameter to the r.Run()
function, like this: r.Run(":3000")
.
E2E Test
we can use curl
to test the application.
curl http://localhost:8080 |
we should see the response:
{"message":"Hello World"} |