This is the 14th day of my participation in the November Gwen Challenge. The details of the event are as follow: The last Gwen Challenge 2021 Hello everyone, I am Nuts, my official account “Nut Front”,

Gin framework introduction and environment construction

Introduction of Gin

Gin is a golang microframework with elegant packaging, API friendly, and clear source code annotations. Version 1.0 has been released. It is fast and flexible, fault-tolerant and convenient. For Golang, web frameworks are far less dependent than Python, Java, etc. Its own NET/HTTP is simple enough and performs very well. Frameworks are more like collections of common functions or tools. Not only can framework development save time for many of the usual encapsulation, but it also helps with the team’s coding style and specification.

The Gin framework is open source, and you can download the source library on Github for instructions. Gin source code library address: github.com/gin-gonic/g…

The Gin Framework has an official website with introduction and learning materials about Gin. Official website: gin-gonic.com/

Gin features and features

  • Speed: The first reason Gin has been adopted by many organizations and teams is because of its speed and superior performance.
  • Middleware: And iris types, gin supports middleware operations when processing requests, facilitating coding processing.
  • Routing: Gin provides a simple route parsing function, including routing group parsing.
  • Built-in rendering: Gin supports rendering in a variety of data formats, including JSON, XML, and HTML, and provides a convenient API for manipulation.

Learning document

Gin official provides reference documentation for us developers. The learning document address is gin-gonic.com/zh-cn/docs/

Gin development environment setup

Environmental requirements

Gin framework requires go language version 1.6 and above. You can use the Go Version command to check whether your GO language version meets requirements.

Install the GIN framework library

Install the GIN framework with the go get command:

go get -u github.com/gin-gonic/gin
Copy the code

After the installation, you can find the gin-gonic directory in the src/github.com directory of the current system’s $GOPATH directory. This directory contains the source code for the gin framework.

Once installed, we can use gin to write a simple Hello World program. Use gin.

Hello Wrold

coding

Create a new project GinHelloWrold and create a main.go program with the following contents:

func main() { r := gin.Default() r.GET("/hello", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "Pong"})}) r.R UN () / / listening and 0.0.0.0: start the service on 8080}Copy the code

run

Run the following command:

go run main.go
Copy the code

Run with a specified port number

server := gin.Default()
...
server.Run(":8090")
Copy the code

That’s all for today’s article. If you like it, you can give it a thumbs up