这是 astaxie 书中的简单形式,当我尝试 '/login' 时,我得到
No Data received { Unable to load the webpage because the server sent no data. Error code: ERR_EMPTY_RESPONSE }
这是代码:
main.go
package main
import (
"fmt"
"html/template"
"net/http"
)
func sayhelloName(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello astaxie!") // write data to response
}
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) //get request method
if r.Method == "GET" {
t, err :=template.New("").Parse(loginHtml)
if err != nil {
panic(err)
}
const loginHtml = `
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
Username:<input type="text" name="username">
Password:<input type="password" name="password">
<input type="submit" value="Login">
</form>
</body>
</html>
`
} else {
r.ParseForm()
// logic part of log in
fmt.Println("username:", r.PostFormValue("username"))
fmt.Println("password:", r.PostFormValue("password"))
}
}
func main() {
http.HandleFunc("/", sayhelloName) // setting router rule
http.HandleFunc("/login", login)
http.ListenAndServe(":9090", nil) // setting listening port
}
#login.gtpl
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
Username:<input type="text" name="username">
Password:<input type="password" name="password">
<input type="submit" value="Login">
</form>
</body>
</html>
Any idea??
相关分类