我知道在 PHP 网络应用程序中,如果您使用 header('Location: http://somewhere.com ') 进行重定向,但您没有 die()/exit(),则始终执行并显示 header 函数之后的代码。我想知道在 Go 中使用 http.Redirect(w, r, "/somewhere", 302) 时是否会发生同样的情况。如果是,如何避免。
最佳答案
http.Redirect 函数写入一个完整的响应。程序在调用 http.Redirect 后继续执行。如果对 http.Redirect 的调用不是函数的最后一行,那么应用程序通常会在调用后立即从处理程序返回。
func serveFoo(w http.ResponseWriter, r *http.Request) {
...
http.Redirect(w, r, "/somewhere", 302)
return
...
}
关于安全地进行 http 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47643065/