http.FileServer直接暴露./static会404,因路径未剥离前缀导致查找./static/static/style.css;正确做法是用http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))。
http.FileServer 直接暴露 ./static 会 404?常见错误是直接写 http.Handle("/static/", http.FileServer(http.Dir("./static"))),但浏览器访问 /static/style.css 仍返回 404。根本原因是 http.FileServer 会把请求路径(如 /static/style.css)完整拼到文件系统路径上,试图读取 ./static/static/style.css —— 多了一层 static。
正确做法是用 http.StripPrefix 剥离前缀:
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
./static 目录真实存在且有读权限StripPrefix 的参数要和 Handle 的注册路径完全一致(结尾斜杠不能少)./dist/assets),就改用 http.Dir("./dist/assets"),别硬塞路径到 URL 前缀里http.FileServer 默认不压缩,浏览器收到的仍是原始体积。Go 标准库没内置压缩中间件,需手动包装 ResponseWriter 或用第三方包。最轻量的方案是使用 github.com/gorilla/handlers.CompressHandler:
import "github.com/gorilla/handlers"
fs := http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))
http.Handle("/static/", handlers.CompressHandler(fs))
favicon.ico),可能得不偿失.js、.css、.html 等文本类型压缩,可用 handlers.CompressHandlerLevel 配合自定义 gzip.BestSpeed
Accept-Encoding + Content-Type 的 wrapper,但要注意 WriteHeader 和 Write 的顺序问题go run 不生效?Go 编译后二进制运行时加载的是启动时刻的文件快照,修改 ./static 下的 CSS/JS 不会自动刷新。这不是缓存问题,而是文件读取发生在运行时,但浏览器可能因强缓存(Cache-Control: max-age=31536000)拒绝重载。
http.FileServer 包一层,覆盖 modtime 并设置短缓存头fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
fs.ServeHTTP(w, r)
})))air 或 reflex)监听文件变化并重启进程,但静态资源本身无需重新编译,所以更推荐前端用 esbuild --watch 或 vite 单独托管 dev serverhttp.FileServer 托管静态资源?标准库的 FileServer 没有并发限流、连接复用

/api/ 路由;Nginx 将 /static/、/assets/ 等路径直接映射到磁盘或 CDNhttp.ServeMux + http.FileServer 组合,并通过 http.TimeoutHandler 加超时保护http.Dir 接收的是相对路径,一旦二进制移动位置,./static 就失效;应改用绝对路径(如 filepath.Join(os.Getenv("APP_ROOT"), "static"))