package main
import (
. "fmt"
"html/template"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", index)
http.HandleFunc("/login", login)
err := http.ListenAndServe(":8888", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func index(response http.ResponseWriter, request *http.Request) {
request.ParseForm()
if request.Method == "GET" {
t, err := template.ParseFiles("login.gtpl")
log.Println(t.Execute(response, err))
}
}
func login(response http.ResponseWriter, request *http.Request) {
if request.Method == "POST" {
request.ParseForm()
for k, v := range request.Form {
Println(k, v)
}
}
}
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="登录">
</form>
</body>
</html>