Notes | GitHub | CV | GPG key

The Go standard library routing improvements

Published:
Tags: Go

Go 1.22 ships with router Enhancements. The net/http.ServeMux can now match requests by method, host and a simple path wildcard.

With the new ServeMux, it is no longer necessary to struggle to find the best routing method. For most cases, standard library should be the best choice. And with the next release, you can align your declarations with any number of spaces.

func run() {
	rt := http.NewServeMux()

	rt.Handle(`POST /users`, &demoHandler{info: "create user"})
	rt.Handle(`GET  /users/{name}`, &demoHandler{info: "show user"})
	rt.Handle(`GET  /users/{name}/profile`, &demoHandler{info: "show user profile"})

	_ = http.ListenAndServe("localhost:8000", rt)
}

type demoHandler struct {
	info string
}

func (h *demoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, h.info, r.PathValue("name"))
}
% curl localhost:8000/users
Method Not Allowed
% curl localhost:8000/users -X POST
create user
% curl localhost:8000/users/andy
show user andy
% curl localhost:8000/users/andy/profile
show user profile andy
% curl localhost:8000/users/andy/profile -X POST
Method Not Allowed