HUWWW
该Redirect::back()函数使用Referer客户端(浏览器)指定的 HTTP 标头中的 URL。您可以像 一样访问此标头值r.Header.Get("Referer"),但该http.Request类型还提供了一个Request.Referer()返回此Referer字段值的直接方法,该方法还处理其两种可能的形式("Referer"和"Referrer")。这是您可以模仿“返回”行为的方式:func PostHandler(w http.ResponseWriter, r *http.Request) { // Process form, then: if rf := r.Referer(); rf != "" { http.Redirect(w, r, rf, http.StatusSeeOther) return } // No Referer specified, supply your own response // or redirect to a default / home page http.Redirect(w, r, "/", http.StatusSeeOther)}如果您想从许多处理程序中调用它,您可以在辅助函数中捕获此功能:func redirectBack(w http.ResponseWriter, r *http.Request) { if rf := r.Referer(); rf != "" { http.Redirect(w, r, rf, http.StatusSeeOther) return } // No Referer specified, supply your own response // or redirect to a default / home page http.Redirect(w, r, "/", http.StatusSeeOther)}然后使用它:func PostHandler(w http.ResponseWriter, r *http.Request) { // Process form, then: redirectBack(w, r)}