百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

michi

> 编程语言
开源

michi 是一个真正 100% 兼容 Go Web 应用程序的 net/http 路由器。

99 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

michi 是一个真正 100% 兼容 Go Web 应用程序的 net/http 路由器。

Features

  • 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

Why michi?

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.

About michi(道)

  • michi(道) means routes in Japanese.

Getting Started

go get -u github.com/go-michi/michi
go
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.

Migrating to michi(http.ServeMux) from chi

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.

import michi package

This change is due to michi.

diff
- import  "github.com/go-chi/chi"
+ import  "github.com/go-michi/michi"

func main() {
-   r := chi.NewRouter()
+   r := michi.NewRouter()
}

Use Handle or HandleFunc method instead of Get, Post, Put, Delete, Patch, Options, Head method

This change is due to http.ServeMux.

diff
func main() {
-   r.Get("/user/{id}", userHandler)
+   r.HandleFunc("GET /user/{id}", userHandler)
}

Use http.Request.PathValue method

This change is due to http.ServeMux.

diff
func Handler(w http.ResponseWriter, r *http.Request) {
-   id := chi.URLParam(r, "id")
+   id := r.PathValue("id")
}

Use {$} suffix for exact match

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
diff
func main() {
-   r.Handle("/a/", userHandler)
+   r.Handle("/a/{$}", userHandler)
}

Sub Router

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.

go
// chi
func main() {
   r := chi.NewRouter()
   // omit /a/ path
   r.Handle("/hello", handler("hello"))
   r2 := chi.NewRouter()
   r2.Mount("/a", r)
 }
go
// michi
func main() {
    r := michi.NewRouter()
    // can't omit /a/ path
    r.Handle("/a/hello", handler("hello"))
    r2 := michi.NewRouter()
    r2.Handle("/a/", r)
}
diff
func main() {
-   r.Handle("/hello", handler("hello"))
+   r.Handle("/a/hello", handler("hello"))
-   r2.Mount("/a", r)
+   r2.Handle("/a/", r)
 }

or using Route

go
// michi
func main() {
    r := michi.NewRouter()
    // can't omit /a/ path
    r.Route("/a", func(r michi.Router) {
        r.Handle("/hello", handler("hello"))
    })
}

Support version

michi only supports Go 1.22 or later and the two latest versions. Currently, supports Go 1.22.

Reference

  • https://go.dev/blog/routing-enhancements
  • https://pkg.go.dev/net/http#ServeMux

Credits

  • Peter Kieltyka for https://github.com/go-chi/chi
    • michi's middleware interface from chi.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Go

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言