Introduction to Gin Framework — Day one
1. Introduction
Gin is a Web framework written in the Go language, similar to Martini but with better performance API frameworks.
1.1 features
- Fast: Radix tree based routing, small memory footprint, no reflection, predictable API performance
- Support for middleware: Incoming HTTP requests can be handled by a set of middleware and eventual operations. For example, Logger, GZIP…
- Crash processing: Gin can catch a panic that occurs in an HTTP request and recover it. That way, your server will always be available. For example, you can report this panic! To Sentry.
- JSON validation: Gin can parse and validate the requested JSON, for example by checking for the presence of the required value.
- Routing groups: Better organize routes. Whether authorization is required, different API versions… In addition, these groups can be nested indefinitely without degrading performance.
- Error management: Gin provides a convenient way to collect all errors that occur during HTTP requests. Finally, middleware can write them to log files, databases and send them over the network.
- Built-in Rendering: Gin provides an easy-to-use API for JSON, XML and HTML rendering.
- Scalability: Creating a new middleware is very simple
1.2 installation
Gin framework can be installed according to Go language Gin framework installation shelgi blog -CSDN blog Gin framework download
Introducing Gin is also very simple: import “github.com/gin-gonic/gin”
Net/HTTP is also introduced when we need to use response constants such as http.statusok
import net/http
main.go
package main import "github.com/gin-gonic/gin" func main() { r := gin.Default() r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "Hello Gin!" ,})}) r.run () // Listen and start the service at 0.0.0.0:8080}Copy the code
2. Hot start of Gin
I looked around and the Gin framework itself doesn’t seem to have hot start support. Since it is not supported by itself, we sought outside help and found that most solutions are through AIR, but we need to install AIR and configure air.conf in the project. I’m a lazy person who always chooses the easiest and most efficient way to implement requirements. If you’re one of those people, check out the following tips
Making address: https://github.com/codegangsta/ginCopy the code
Gin is a simple command-line utility for reloading Go Web applications in real time. As long as GIN is running in the application directory, network applications provide GIN as a proxy. When gin detects changes, it will automatically recompile your code. Your application will restart the next time it receives an HTTP request. But if there’s a compilation error or something goes wrong, it’s still going to strike.
The installation
go get github.com/codegangsta/gin
After the installation, let’s test whether the installation is successful
gin -h
Then try to hot deploy the code and start the service
gin run main.go
The default port for gin discovery is 3000
Then change message to Hello Shelgi and refresh the page
This is the content of the first day, easy to use, and then slowly explain the use of the framework.