先来看一个表单递交的例子,我们有如下的表单内容,命名成文件login.html(放入当前新建项目的目录里面)
<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>
上面递交表单到服务器的/login,当用户输入信息点击登陆之后,会跳转到服务器的路由login里面,我们首先要判断这个是什么方式传递过来,POST还是GET呢?http包里面有一个很简单的方式就可以获取form信息
package main import ( "fmt" "html/template" "log" "net/http" "strings" ) func saygo(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello World!\n") } func login(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { t, _ := template.ParseFiles("login.html") t.Execute(w, nil) } else { w.Header().Set("Content-Type", "text/html") r.ParseForm() username := strings.Join(r.Form["username"], "") fmt.Fprintf(w, username+"<br/>") password := strings.Join(r.Form["password"], "") fmt.Fprintf(w, password+"<br/>") } } func main() { http.HandleFunc("/", saygo) http.HandleFunc("/login/", login) err := http.ListenAndServe(":9090", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }
通过以上简单的代码,我们就可以处理表单的基本信息.
hole of the celebrities vol
weight loss tips Top Places to Shop for Casual Petites
Apply for a visa
how to lose weight fastHigh collars a fashion recycle
how to lose weight fastHigh collars a fashion recycle