go语言实现处理表单输入
login.html
<html>
<head><title></title></head>
<body>
<formaction="http://localhost:9090/login"method="post">
用户名:<inputtype="text"name="username">
密 码:<inputtype="text"name="password">
<inputtype="submit"value="登录">
</form>
</body>
</html>
main.go
packagemain
import(
"fmt"
"html/template"
"log"
"net/http"
"strings"
)
funcsayHelloName(whttp.ResponseWriter,r*http.Request){
//解析url传递的参数
r.ParseForm()
//在服务端打印信息
fmt.Println(r.Form)
fmt.Println("path",r.URL.Path)
fmt.Println("Scheme",r.URL.Scheme)
fmt.Println(r.Form["url_long"])
fork,v:=ranger.Form{
fmt.Println("key:",k)
//join()方法用于把数组中的所有元素放入一个字符串。
//元素是通过指定的分隔符进行分隔的
fmt.Println("val:",strings.Join(v,""))
}
//输出到客户端
fmt.Fprintf(w,"helloastaxie!")
}
funclogin(whttp.ResponseWriter,r*http.Request){
fmt.Println("method:",r.Method)
ifr.Method=="GET"{
t,_:=template.ParseFiles("login.html")
//执行解析模板
//func(t*Template)Execute(wrio.Writer,datainterface{})error{
t.Execute(w,nil)
}else{
r.ParseForm()
fmt.Println("username:",r.Form["username"])
fmt.Println("password:",r.Form["password"])
}
}
funcmain(){
//设置访问路由
http.HandleFunc("/",sayHelloName)
http.HandleFunc("/login",login)
//设置监听端口
err:=http.ListenAndServe(":9090",nil)
iferr!=nil{
log.Fatal("ListenAndserve:",err)
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下