Now Reading
Higher HTTP server routing in Go 1.22 – Eli Bendersky’s web site

Higher HTTP server routing in Go 1.22 – Eli Bendersky’s web site

2023-10-16 07:38:14

An exciting proposal is
anticipated to land in Go 1.22 – enhancing the pattern-matching capabilities of
the default HTTP serving multiplexer within the internet/http package deal.

The prevailing multiplexer (http.ServeMux) presents rudimentary path matching, however
not a lot past that. This led to a cottage trade of third occasion libraries
to implement extra highly effective capabilities. I’ve explored these choices in my
REST Servers in Go sequence, in components 1
and 2.

The brand new multiplexer in 1.22 goes to considerably bridge the hole from third
occasion packages by offering superior matching. On this quick put up
I will present a fast introduction to the brand new multiplexer (mux).
I will additionally revisit the instance from the REST Servers in
sequence and evaluate how the brand new stdlib mux fares towards gorilla/mux.

A cartoony go gopher holding a multiplexer

Utilizing the brand new mux

If you happen to’ve ever used a third occasion mux / router package deal for Go (like
gorilla/mux), utilizing the brand new commonplace mux goes to be simple and
acquainted. Begin by studying its documentation – it is quick and candy.

Let us take a look at a few fundamental utilization examples. Our first instance demonstrates
a few of the new sample matching capabilities of the mux:

package deal foremost

import (

func foremost() {
  mux := http.NewServeMux()
  mux.HandleFunc("GET /path/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "received pathn")

  mux.HandleFunc("/process/{id}/", func(w http.ResponseWriter, r *http.Request) {
    id := r.PathValue("id")
    fmt.Fprintf(w, "dealing with process with id=%vn", id)

  http.ListenAndServe("localhost:8090", mux)

Skilled Go programmers will discover two new options immediately:

  1. Within the first handler, the HTTP technique (GET on this case) is specified
    explicitly as a part of the sample. Because of this this handler will solely
    set off for GET requests to paths starting with /path/, not for
    different HTTP strategies.
  2. Within the second handler, there is a wildcard within the second path part
    {id}, one thing that wasn’t supported earlier than. The wildcard will match
    a single path part and the handler can then entry the matched worth
    via the PathValue technique of the request.

Since Go 1.22 hasn’t been launched but, I like to recommend working this pattern with
gotip. Please see the complete code sample
with full directions for working this. Let’s take this server for a experience:

And in a separate terminal we are able to problem some curl calls to check it:

$ curl localhost:8090/what/
404 web page not discovered

$ curl localhost:8090/path/
received path

$ curl -X POST localhost:8090/path/
Technique Not Allowed

$ curl localhost:8090/process/f0cd2e/
dealing with process with id=f0cd2e

Observe how the server rejects a POST request to /path/, whereas the (default
for curl) GET request is allowed. Observe additionally how the id wildcard will get
assigned a worth when the request matches. As soon as once more, I encourage you to evaluate
the documentation of the new ServeMux. You will study further
capabilities like matching trailing paths to a wildcard with {id}...,
strict matching of a path finish with {$}, and different guidelines.

Explicit care within the proposal was given to potential conflicts between
totally different patterns. Contemplate this setup:

mux := http.NewServeMux()
mux.HandleFunc("/process/{id}/standing/", func(w http.ResponseWriter, r *http.Request) {
        id := r.PathValue("id")
        fmt.Fprintf(w, "dealing with process standing with id=%vn", id)
mux.HandleFunc("/process/0/{motion}/", func(w http.ResponseWriter, r *http.Request) {
        motion := r.PathValue("motion")
        fmt.Fprintf(w, "dealing with process 0 with motion=%vn", motion)

And suppose the server receives a request for /process/0/standing/ — which
handler ought to it go to? It matches each! Subsequently, the brand new ServeMux
documentation meticulously describes the priority guidelines for patterns, alongside
with potential conflicts. In case of a battle, the registration panics.
Certainly, for the instance above we get one thing like:

panic: sample "/process/0/{motion}/" (registered at sample-conflict.go:14) conflicts with sample "/process/{id}/standing/" (registered at sample-conflict.go:10):
/process/0/{motion}/ and /process/{id}/standing/ each match some paths, like "/process/0/standing/".
However neither is extra particular than the opposite.
/process/0/{motion}/ matches "/process/0/motion/", however /process/{id}/standing/ would not.
/process/{id}/standing/ matches "/process/id/standing/", however /process/0/{motion}/ would not.

The message is detailed and useful. If we encounter conflicts in complicated
registration schemes (particularly when patterns are registered in a number of locations
within the supply code), such particulars might be a lot appreciated.

Redoing my process server with the brand new mux

The REST Servers in Go sequence implements a easy server for a process/todo-list
software in Go, utilizing a number of totally different approaches. Part 1
begins with a “vanilla” commonplace library strategy, and Part 2
reimplements the identical server utilizing the gorilla/mux router.

Now is a good time to reimplement it as soon as once more, however with the improved mux
from Go 1.22; it is going to be notably fascinating to check the answer to
the one utilizing gorilla/mux.

The complete code for this challenge is available here.
Let us take a look at a couple of consultant code samples, beginning with the sample
registration :

See Also

mux := http.NewServeMux()
server := NewTaskServer()

mux.HandleFunc("POST /process/", server.createTaskHandler)
mux.HandleFunc("GET /process/", server.getAllTasksHandler)
mux.HandleFunc("DELETE /process/", server.deleteAllTasksHandler)
mux.HandleFunc("GET /process/{id}/", server.getTaskHandler)
mux.HandleFunc("DELETE /process/{id}/", server.deleteTaskHandler)
mux.HandleFunc("GET /tag/{tag}/", server.tagHandler)
mux.HandleFunc("GET /due/{12 months}/{month}/{day}/", server.dueHandler)

Similar to within the gorilla/mux pattern, right here we use particular HTTP strategies
to route requests (with the identical path) to totally different handlers; with the older
http.ServeMux, such matchers needed to go to the identical handler, which might then
resolve what to do based mostly on the tactic.

Let’s additionally take a look at one of many handlers:

func (ts *taskServer) getTaskHandler(w http.ResponseWriter, req *http.Request) {
  log.Printf("dealing with get process at %sn", req.URL.Path)

  id, err := strconv.Atoi(req.PathValue("id"))
  if err != nil {
    http.Error(w, "invalid id", http.StatusBadRequest)

  process, err := ts.retailer.GetTask(id)
  if err != nil {
    http.Error(w, err.Error(), http.StatusNotFound)

  renderJSON(w, process)

It extracts the ID worth from req.PathValue("id"), equally to the Gorilla
strategy; nonetheless, since we do not have a regexp specifying that {id} solely
matches integers, we’ve got to concentrate to errors returned from

All and all, the tip result’s remarkably much like the answer that makes use of
gorilla/mux from part 2.
The handlers are a lot better separated than within the vanilla stdlib strategy,
as a result of the mux now can do far more refined routing, with out leaving many
of the routing choices to the handlers themselves.


“Which router package deal ought to I take advantage of?” has at all times been a FAQ for newbie Go
programmers. I consider the widespread solutions to this query will shift after
Go 1.22 is launched, as many will discover the brand new stdlib mux adequate for his or her
wants with out resorting to third occasion packages.

Others will follow acquainted third occasion packages, and that is completely advantageous.
Routers like gorilla/mux nonetheless present extra capabilities than the usual
library; on prime of it, many Go programmers go for light-weight frameworks like
Gin, which give a router but additionally further instruments for constructing internet backends.

All in all, that is definitely a constructive change for all Go customers. Making the
commonplace library extra succesful is a internet constructive for your entire neighborhood,
whether or not individuals use third occasion packages or stick to only the usual library.

Source Link

What's Your Reaction?
In Love
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top