【发布时间】:2018-02-09 01:06:42
【问题描述】:
我正在通过 go 服务 index.html。但是,根据将通过页面发送的某些参数,go 应该成功地重定向到不同的页面。尝试执行代码时出现以下错误。
http:多个 response.WriteHeader 调用
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:])
fmt.Println(r.FormValue("login"))
if r.FormValue("signup") == "signup" {
signup(w, r)
} else if r.FormValue("login") == "login" {
if login(w, r) {
if r.Method == "POST" {
fmt.Println("I m here")
http.Redirect(w, r, "http://localhost:8080/home.html" (http://localhost:8080/home.html') , http.StatusSeeOther)
}
}
}
})
var port string
if port = os.Getenv("PORT"); len(port) == 0 {
port = DEFAULT_PORT
}
log.Fatal(http.ListenAndServe(":"+port, nil))
}
【问题讨论】:
-
您无法使用 ServeFile,然后尝试重定向。每个 HTTP 请求都可以只回答一次,发送重定向就是一个回答。
-
在stackoverflow上搜索你的错误(上面加粗),你会发现很多相同问题的插图。