Original author, public number [programmer reading], welcome to pay attention to the public number, reprint the article please indicate the source oh.

While the Go standard library, NET/HTTP, provides great support for developing Web applications using Go, a Web framework is a better choice for teams that want to quickly develop and bring projects online.

Go in the community, there are a lot of very good Web framework, such as Gin, Iris, Echo, Martini, Revel Beego framework and people development.

The advantages of Gin

  • Fast: Based onRadixTree routing, very powerful performance.
  • Support for middleware: Many middleware are built in, such asLogger.Gzip.AuthorizationAnd so on.
  • Crash recovery: You can catch program crashes caused by panic and keep the Web service running.
  • JSON validation: You can validate the requestJSONData format.
  • Routing group: Supports routing group (RouteGroup) to make routing easier to organize.
  • Error management mechanism: Can collect errors in the program
  • Multiple data rendering methods: supportedHTML,JSON,YAML,XMLAnd so on.
  • Extensibility: Very simple to extend middleware.
  • Data validator: Data validators are supported and can be customized.

Installation and use

The latest version of Gin is V1.3.0, and the installation process is very simple. However, before installing Gin, you need to install Go1.6 or higher (later versions may be Go1.8 or higher). The following two installation methods are described.

Installed directly

$go get -u github.com/gin-gonic/gin // Use -u to install the latest versionCopy the code

Install using Govendor

Tip: Govendor uses the Go language to develop Go project dependency management tools.

  1. Install Govendor
$ go get github.com/kardianos/govendor
Copy the code
  1. Install the Gin
$govendor init $govendor fetch github.com/gin-gonic/[email protected]Copy the code

A simple example

With Gin installed in the above two ways, let’s take a look at a simple example of how Gin can be used to develop Web applications.

import "github.com/gin-gonic/gin"
func main(){
    r := gin.Default()
    r.GET("/test",func(c *gin.Context){
        c.JSON(200,gin.H{"hello":"world"})
    })
    r.Run()
}
Copy the code

As you can see, developing a Web service using GIN is a straightforward task that can be broken down into four simple steps:

1. Import the GIN package

When we install the Gin framework, Gin is wrapped into a local installation, if you use the go a get command of installation, the package path $GOPATH/src/github.com/gin-gonic/gin, and we only need to use the import command and package can be imported.

import "github.com/gin-gonic/gin"
Copy the code
2. Create a route

Using the gin.Default() method returns an instance of gin.Engine, representing the Default routing Engine.

r := gin.Default()
Copy the code

The gin.Engine created in this way uses Logger and Recovery middleware by default. You can use the gin.New() method to create a default route without any middleware.

r := gin.New()
Copy the code
3. Define methods for handling HTTP

With the default route, we can create methods to handle HTTP requests, using the GET method in our example:

r.GET("/test",func(c *gin.Context){
    c.JSON(200,gin.H{"hello":"world"})})Copy the code

Gin support all general HTTP request method: GET, POST, PUT, PATCH, OPTIONS, HEAD, DELETE, its use is the same as the above example, such as the POST:

r.POST("/test",func(c *gin.Context){
    c.JSON(200,gin.H{"hello":"world"})})Copy the code

Each method handles only the corresponding HTTP request, and Any method can handle Any HTTP request.

r.Any("/test",func(c *gin.Context){
    c.JSON(200,gin.H{"hello":"world"})})Copy the code
4. Listen to the port

Once the request is defined, use the Run() method to listen on the port and start accepting HTTP requests. If the Run() method takes no arguments, the default port to listen on is 8080.

r.Run() //r.Run(": 3000")
Copy the code

summary

Gin is a lightweight framework for Go Web development and is very simple to use. However, before developing with Gin, you still need to have some understanding of the NET/HTTP support that Go natively supports.

Your attention is the biggest encouragement on my writing road!