Write The Perfect REST API With Go 1.22
Embrace the Future: Go 1.22 Improves Routing Capabilities for REST API Development
When I need to develop a REST API, I often use open-source libraries like gorilla/mux. However, when working with non-official libraries, there’s always the risk that they might not be maintained anymore.
Fortunately, Go has realized some aspects that could be improved in handling HTTP and has introduced two major updates for the net/http
package, particularly for http.ServeMux
.
This article will show you how to use these improvements to build a simple REST API in this article. I won’t show you complex and realistic examples in the article for a more effective explanation. Instead, I will use a skeleton of what can be done.
You’ll find a link to my GitHub repository at the end of the article, which I used to do a Proof-of-Concept (PoC) of these new features. So you also have an example that you can run on your PC.
Let’s start looking at the two big steps that have been made.
Method Matching
Imagine creating a REST API to manage a list of notes. You want to read all the notes you have saved. What endpoint do you need?