net/http: ServeMux does not match method patterns ("GET /path" is 404)
With TinyGo 0.42.0, http.ServeMux does not match patterns that start with a method (Go 1.22's "GET /path"). Path wildcards do work.
package main
import (
"fmt"
"net/http"
"net/http/httptest"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("GET /a", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "method pattern") })
mux.HandleFunc("/b/{id}", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "wildcard ", r.PathValue("id")) })
mux.HandleFunc("/c", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "plain") })
for _, target := range []string{"/a", "/b/7", "/c"} {
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, httptest.NewRequest("GET", target, nil))
fmt.Println(target, rec.Code, rec.Body.String())
}
}go run . (Go 1.27.1):
/a 200 method pattern
/b/7 200 wildcard 7
/c 200 plaintinygo run . (TinyGo 0.42.0, darwin/arm64 host; the same on -target wasm):
/a 404 404 page not found
/b/7 200 wildcard 7
/c 200 plainThe registration doesn't panic; the request just isn't matched. src/net/http/pattern.go is present in the TinyGo tree ("copied and modified from Go 1.26.2"), so it looks like ServeMux.Handle / match in server.go doesn't use it for the method part.
Why it matters: libraries that register "METHOD /path" return 404 for every route. I hit it with Huma's humago adapter on Cloudflare Workers and worked around it by matching routes myself (https://github.com/joeblew999/orpc-api/blob/main/api-go/humaworkers/humaworkers.go).
Source: tinygo-org/tinygo