// object project main.go
package main
import (
"html/template"
"net/http"
)
func main() {
http.HandleFunc("/", Hey)
http.ListenAndServe(":8080", nil)
}
const tpl = `
<html>
<head>
<title>表单传输</title>
</head>
<body>
<form method="post">
username:<input type="text" name="username"></p>
password:<input type="password" name="password">
<button type="submit">Submit</button>
</form>
</body>
</html>
`
func Hey(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
//获取模板文件
t := template.New("hey")
//解析模板文件
t.Parse(tpl)
//输出文件
t.Execute(w, nil)
} else {
//将用户名和密码输出
w.Write([]byte("用户名:" + r.FormValue("username")))
w.Write([]byte("密码:" + r.FormValue("password")))
}
}