我想到的一件事可能对您有所帮助,您可以创建自己的ServeMux。我在您的示例中添加了内容,使chttp是一个ServeMux,您可以为其提供静态文件。然后,HomeHandler会检查它是否应该提供文件。我只是检查一个“。”
但您可以做很多事情。只是一个想法,可能不是您想要的。
package mainimport ( "fmt" "net/http" "strings")var chttp = http.NewServeMux()func main() { chttp.Handle("/", http.FileServer(http.Dir("./"))) http.HandleFunc("/", HomeHandler) // homepage http.ListenAndServe(":8080", nil)}func HomeHandler(w http.ResponseWriter, r *http.Request) { if (strings.Contains(r.URL.Path, ".")) { chttp.ServeHTTP(w, r) } else { fmt.Fprintf(w, "HomeHandler") } }


