Go 1.22 http mux: serve an handle and a FS on the same path

  Kiến thức lập trình

In a Go web application I have an embedded FS that contains either static files and templates files.

An index func func index(w http.ResponseWriter, r *http.Request) is working with parsing and executing a template.

How could I serve both on the / path?
GET / will serve index
GET/{anythingelse} will be served from FS

//go:embed assets
var staticFiles embed.FS

var staticFS = fs.FS(staticFiles)
htmlContent, _ := fs.Sub(staticFS, "assets")
fs := http.FileServer(http.FS(htmlContent))

mux := http.NewServeMux()
mux.Handle("GET /", fs)
mux.HandleFunc("GET /", index)

julienschmidt/httprouter has a router.NotFound. How can I do the same?

LEAVE A COMMENT