“This is the 14th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
directory
- preface
- The body of the
- Define GET, POST, PUT, PATCH, DELETE, and OPTIONS interfaces
- 2. Parse the parameters in path
- At the end
preface
Gin is an HTTP Web framework implemented using the pure Golang language. Gin is now widely used for its simple interface design and high performance. The last article introduced you to the basics of Gin, and today we’ll take a closer look at Gin in terms of interface definition and parameter parsing.
The body of the
Define GET, POST, PUT, PATCH, DELETE, and OPTIONS interfaces
package main
import "github.com/gin-gonic/gin"
func main(a) {
// Create a default gin router
router := gin.Default()
// Define a service interface of type Get
router.GET("/someGet", getting)
// Define a Post service interface
router.POST("/somePost", posting)
// Define a service interface of type Put
router.PUT("/somePut", putting)
// Define a service interface of type DELETE
router.DELETE("/someDelete", deleting)
// Define a Patch service interface
router.PATCH("/somePatch", patching)
// Define a service interface of type Head
router.HEAD("/someHead", head)
// Define a service interface of type Options
router.OPTIONS("/someOptions", options)
// Start the service, listening on PORT 8080 by default, or through the PORT environment variable
router.Run()
}
Copy the code
With simple coding, gIN-based servers support service interfaces for multiple types of requests. In addition, when the service is started, we can set the port that the service listens on by specifying the port directly, for example:
router.Run(": 3001")
Copy the code
In the above statement, we know that the service listens on port 3001.
2. Parse the parameters in path
The following code examples show how to resolve several cases where there are parameters in path.
package main
import "github.com/gin-gonic/gin"
func main(a) {
router := gin.Default()
// matches /user/ John, but not /user/ and /user requests
router.GET("/user/:name".func(c *gin.Context) {
name := c.Param("name")
c.String(http.StatusOK, "Hello %s", name)
})
// Can match /user/ John/and /user/ John /send
router.GET("/user/:name/*action".func(c *gin.Context) {
name := c.Param("name")
action := c.Param("action")
message := name + " is " + action
c.String(http.StatusOK, message)
})
// For each matched request, the context holds the route definition
router.POST("/user/:name/*action".func(c *gin.Context) {
c.FullPath() == "/user/:name/*action" // true
})
router.Run(": 8080")}Copy the code
As you can see, there is a lot of detail to pay attention to when parsing the parameters in path.
At the end
Well, that’s all for today’s introduction of Gin in interface definition and parameter parsing. Please look forward to the rest of the content, thank you!
About the author: Hello, everyone, I am Liuzhen007, an audio and video technology fan, CSDN blog expert, Huawei Cloud community cloud sharing expert, signed the author, welcome to follow me to share more dry products!