- True 100% compatible with net/http - http.ServerMux, http.Handler and http.HandlerFunc
- Enhanced http.ServeMux - After Go 1.22, it is possible to use http method and path values
- API like chi - Route, Group, With and middlewares
- No external dependencies - Only use standard package
- Lightweight - Only 160 lines
- Performance - Fast michi == http.ServeMux
After Go 1.22, HTTP routing in the standard library is now more expressive. The patterns used by net/http.ServeMux have been enhanced to accept methods and wildcards. But these were already in 3rd party Routing libraries. So, we removed these overlapping features and provide a lack of http.ServeMux features.
- michi(道) means routes in Japanese.
go get -u github.com/go-michi/michi
package main
import (
"fmt"
"net/http"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-michi/michi"
)
func main() {
r := michi.NewRouter()
r.Use(middleware.Logger)
r.HandleFunc("POST /a/{id}/{$}", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Hello " + req.PathValue("id")))
})
http.ListenAndServe(":3000", r)
}
Before using michi, read the http.ServeMux GoDoc. For more detailed usage, check the Example in michi GoDoc.
There are several changes, but rather than changing from chi to michi, it's about changing from chi to http.ServeMux. Therefore, what you need to understand is how to use the standard library's http.ServeMux, and the knowledge specific to michi is kept to a minimum.
This change is due to michi.
- import "github.com/go-chi/chi"
+ import "github.com/go-michi/michi"
func main() {
- r := chi.NewRouter()
+ r := michi.NewRouter()
}
This change is due to http.ServeMux.
func main() {
- r.Get("/user/{id}", userHandler)
+ r.HandleFunc("GET /user/{id}", userHandler)
}
This change is due to http.ServeMux.
func Handler(w http.ResponseWriter, r *http.Request) {
- id := chi.URLParam(r, "id")
+ id := r.PathValue("id")
}
This change is due to http.ServeMux.
- with
{$}
, routing pattern match rule is same as chi/a/{$}
matches request/a/
- without
{$}
, routing pattern match rule is same as old http.ServeMux/a/
matches request/a/
and/a/b
func main() {
- r.Handle("/a/", userHandler)
+ r.Handle("/a/{$}", userHandler)
}
This change is due to http.ServeMux. http.ServeMux doesn't have Mount method, use Handle method instead of Mount method. Handle method can't omit parent path.
// chi
func main() {
r := chi.NewRouter()
// omit /a/ path
r.Handle("/hello", handler("hello"))
r2 := chi.NewRouter()
r2.Mount("/a", r)
}
// michi
func main() {
r := michi.NewRouter()
// can't omit /a/ path
r.Handle("/a/hello", handler("hello"))
r2 := michi.NewRouter()
r2.Handle("/a/", r)
}
func main() {
- r.Handle("/hello", handler("hello"))
+ r.Handle("/a/hello", handler("hello"))
- r2.Mount("/a", r)
+ r2.Handle("/a/", r)
}
or using Route
// michi
func main() {
r := michi.NewRouter()
// can't omit /a/ path
r.Route("/a", func(r michi.Router) {
r.Handle("/hello", handler("hello"))
})
}
michi only supports Go 1.22 or later and the two latest versions. Currently, supports Go 1.22.
- Peter Kieltyka for https://github.com/go-chi/chi
- michi's middleware interface from chi.