[go]获取表单的方法

// 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")))
    }

}

 

posted @ 2018-01-15 17:34  Limo1996  阅读(962)  评论(0)    收藏  举报