Gin the next day
1.RESTful API
Simply put, REST means that when a client interacts with a Web server, four requests in the HTTP protocol represent different actions. It is an API design concept for Internet applications: URL locates resources, HTTP describes operations
- GET_to get resources
- POST_to create a new resource
- PUT_to update resources
- DELETE_to delete resources.
As long as the API program follows the REST pane, it can be called the RESTful API. _In the architecture of front-end and back-end separation, front-end and back-end basically interact through the RESTful API.
Here's a Gin implementation
package main import ( "github.com/gin-gonic/gin" "net/http" ) func main(){ r:=gin.Default() r.GET("/",func(c *gin.Context){ c.JSON(http.StatusOK,gin.H{ "message":"GET", }) }) r.POST("/post",func(c *gin.Context){ c.JSON(http.StatusOK,gin.H{ "message":"POST", }) }) r.PUT("/put",func(c *gin.Context){ c.JSON(http.StatusOK,gin.H{ "message":"PUT", }) }) r.DELETE("/delete",func(c *gin.Context){ c.JSON(http.StatusOK,gin.H{ "message":"DELETE", }) }) r.Run() }
2. API parameter issues
We can get API parameters using the Param method of Context
Let's start with a demo code
package main import ( "github.com/gin-gonic/gin" "net/http" "strings" ) func main(){ r:=gin.Default() r.GET("/user/:name/*action",func(c *gin.Context){ name:=c.Param("name") action:=c.Param("action") // strings.Trim() returns the result after removing all containing Cutsets action=strings.Trim(action,"/") c.String(http.StatusOK,"name:"+name +"\naction:"+ action) }) r.Run() }
But I don't know if you have any doubts about it. The place where the API parameters are set in the url is used: what does and * mean, and I was also confused at that time, but none of the documents explained the problem, so I read the source code to explain it.
First click c.Param to enter context.go, then see
You can see that this is actually to find the corresponding value according to the key, continue clicking on the ByName() method to get to tree.go
ps.Get() uses the Get() function above, traversing the ps parameter slice to find the corresponding name, returning the corresponding value and true when the name is the same, otherwise returning false;
Looking further up, it is the definition of the Params parameter slice and the declaration of the Param structure, and it is clear that the key-value pair, according to the notes above, understands that Param is a single URL parameter that contains a key and a value.
At the top, I see where we're confused: and*, but by name, I don't know what these two byte slices are. I don't understand why these two most confusing local definitions don't have any notes at all!!!
Now that we're going to dig deeper, tree.go defines these two slices, and there must be something to use later, where you might see what they do. Otherwise, continue flipping backwards to see a function looking for wildcards
Note above, find a wildcard field and check for invalid character names, return -1 if no wildcard is found
This doesn't really solve our problem. Looking at the comment inside the function, the wildcard starts with: parameter or * (get all)
Here we have solved the puzzle by matching only one parameter with a wildcard that starts with * and everything that follows with *.
Now that we know the rules, let's try writing a few test cases
package main import ( "github.com/gin-gonic/gin" "net/http" "strings" ) func main(){ r:=gin.Default() r.GET("/user/:name/:xxx/*action",func(c *gin.Context){ name:=c.Param("name") xxx:=c.Param("xxx") action:=c.Param("action") // strings.Trim() returns the result after removing all containing Cutsets action=strings.Trim(action,"/") c.String(http.StatusOK,"name:"+name+"\nxxx:"+xxx+"\naction:"+ action) }) r.Run() }
If: after *?
package main import ( "github.com/gin-gonic/gin" "net/http" "strings" ) func main(){ r:=gin.Default() r.GET("/user/:name/*action/:xxx",func(c *gin.Context){ name:=c.Param("name") xxx:=c.Param("xxx") action:=c.Param("action") // strings.Trim() returns the result after removing all containing Cutsets action=strings.Trim(action,"/") c.String(http.StatusOK,"name:"+name+"\nxxx:"+xxx+"\naction:"+ action) }) r.Run() }
The compiler did make a mistake, and the catch-all wildcard can only be at the end of the path, which was taken into account by the designer in tree.go, so if you are unfamiliar with the two wildcards, the code you might write will make a joke.