The routing function of the Gin framework is designed based on httprouter. httprouter is a Golang implemented routing component; it uses the data structure of the radix tree (also known as radix trie or compressed prefix tree) to maintain the mapping routing relationship and route quickly through the prefix tree. Simultaneously, the interface implemented by the structure in it can be used as a HttpRouter release.
A radix tree (Radix Tree), also known as PAT a bit tree (Patricia-trie or crit-bit tree), is a more space-efficient prefix tree (Trie Tree). For each radix tree node, if the node is the only subtree, it is merged with the parent node.
HTTP Method
Common related methods ( ) have been encapsulated in the Gin framework, and direct calls will quickly register associated routes. The file location of the source code: HTTP GET, POST, PUT, DELETE, HEAD, …e.t.c.
Startup log output:
Example of calling:
It should be noted that when a route is registered as a specified HTTP method, it must be requested in the same way, otherwise it will return: 404 page not found
Match all HTTP requests
Using Any methods, you can match all HTTP methods (GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE)
The usage example is as follows:
Registration Rules
Definitions
According to the above example, HTTP the rules for registering method routing can be summarized as follows:
path: represents the path.
...HandlerFunc: One or more handler functions that receive requests.
Upload multiple HandlerFunc examples
Request output:
Matching rules
Since the Gin route is used httprouter, and httprouter the path matching rules are arranged as follows:
/path
This type of path will only match /path.
It will not match /path/.
Start the service
Initiate a request
When the registered routing path is: /path, it will not match /path/, and it will be returned when requested with a command Moved Permanently, but it will be returned normally when requested with a browser. Reason:httprouter Automatic redirection is enabled by default and /path/ will be redirected to /path.
:param
:param: are the names of the parameters.
Matching rules
Routing Rules
Request Examples
Parameter Values
/path/:a
/test/finbarrs
a=finbarrs
/path/:a/:b
/test/finbarrs/1
a=finbarrs,b=1
/path/:a/:b/:c
/test/finbarrs/1/2
a=finbarrs,b=1,c=2
Start the service
Initiate requests
*param
Matches from the specified position (including the prefix "/") to the end.