introduction
Go version 1.22 has enhanced the routing function of the net/http package, introducing two new functions: method matching and wildcards. These features allow developers to use pattern strings to express common routes without directly writing Go code.
Method Matching
HTTP methods can now be specified in the pattern string, allowing the router to distinguish different methods in the same URL mode (such as GET, POST, etc.). If there is a route that handles GET requests, it might be written like this before Go 1.22:
("/posts/", handlePost)
Will match all requests starting with /posts/ , and the handlePost function requires additional parsing of the ID and checking the HTTP method.
In Go 1.22, HTTP methods can be specified in routing mode. You can write this way:
("GET /posts/{id}", handlePost2)
Here {id} is a wildcard that will match any part of the path. The handlePost2 function no longer needs to check the HTTP method, because routing has ensured that only GET requests will be matched. The value of a wildcard character can be extracted from the Request object through the PathValue method:
idString := ("id")
Wildcards (Wildcards)
A pattern can contain wildcard characters to match variable path segments. The wildcard {} can match a part of the path, while {...} can match the rest of the path. For example, /files/{pathname...} can match any path after /files/ .
Path prefix matching
If you want to match an exact path, including a slash at the end of the path, you can write it like this:
("/posts/{$}", handlePosts)
This will match /posts/ but will not match /posts or /posts/234.
Priority Rules
When multiple patterns match the same request, it is a challenging question to determine which pattern has priority. Go 1.22 adopts specificity-based rules:
- If a pattern matches the request set is a strict subset of another pattern, it is considered more specific.
- The most specific model has priority.
- If the two patterns overlap but none are more specific, it is considered a conflict and registering the two patterns will cause panic.
For example, /posts/latest is more specific than /posts/{id} because it matches exactly one path, which matches any path starting with /posts/ .
For example, /posts/{id} and /{resource}/latest can both match /posts/latest. These two patterns do not distinguish which one is more preferred, so registering these two routes will raise panic.
compatibility
Go 1.22 strives to maintain compatibility with the old version. The new pattern syntax is a superset of the old syntax, so the code function is still normal after upgrading to 1.22. However, there are some edge cases to be aware of, for example, older versions of Go will treat pattern with braces as literals, while Go 1.22 uses braces for wildcards, which can restore the behavior of older versions by setting the value of the GODEBUG environment variable to httpmuxgo121.
API changes
net/ Two new methods have been added:
- PathValue, used to extract the value of a wildcard from the request path.
- SetPathValue, allows routers outside the standard library to make wildcard matching results available through PathValue.
summary
These enhancements to the net/http package make Go's standard library more powerful in building web services with complex routing requirements, reducing the need for third-party frameworks for many use cases. However, third-party frameworks are still a good choice for applications with more advanced routing requirements. The Go team demonstrates a commitment to meeting user needs and respecting the principles of backward compatibility by integrating these features by studying third-party frameworks, extracting common features and interacting with the community.
The above is the detailed explanation of the routing enhancement function of Go 1.22 for the net/http package. For more information about the routing enhancement of Go1.22 net/http package, please follow my other related articles!