皈依舞
假设您的文件服务器代码类似于文档中的示例:http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("/static"))))您可以编写一个处理程序,通过剥离 ETag 标头并设置Cache-Control: no-cache, private, max-age=0防止缓存(本地和上游代理)来设置适当的缓存标头以防止这种行为:var epoch = time.Unix(0, 0).Format(time.RFC1123)var noCacheHeaders = map[string]string{ "Expires": epoch, "Cache-Control": "no-cache, private, max-age=0", "Pragma": "no-cache", "X-Accel-Expires": "0",}var etagHeaders = []string{ "ETag", "If-Modified-Since", "If-Match", "If-None-Match", "If-Range", "If-Unmodified-Since",}func NoCache(h http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { // Delete any ETag headers that may have been set for _, v := range etagHeaders { if r.Header.Get(v) != "" { r.Header.Del(v) } } // Set our NoCache headers for k, v := range noCacheHeaders { w.Header().Set(k, v) } h.ServeHTTP(w, r) } return http.HandlerFunc(fn)}像这样使用它:http.Handle("/static/", NoCache(http.StripPrefix("/static/", http.FileServer(http.Dir("/static")))))注意:我最初在github.com/zenazn/goji/middleware 上写了这个,所以你也可以导入它,但这是一段简单的代码,我想为后代展示一个完整的例子!