首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >go实现表单提交

go实现表单提交

作者头像
公众号-利志分享
发布2022-04-25 08:21:35
发布2022-04-25 08:21:35
1.3K0
举报
文章被收录于专栏:利志分享利志分享

package main import ( "fmt" "html/template" //支持模板html "log" //打印日志log类 "net/http" "strings" ) func sayHelloName(w http.ResponseWriter, r *http.Request) { r.ParseForm() fmt.Println(r.Form) fmt.Println("path", r.URL.Path) fmt.Println("scheme", r.URL.Scheme) fmt.Println(r.Form["url_long"]) for k, v := range r.Form { fmt.Println("key:", k) fmt.Println("val:", strings.Join(v, "")) } fmt.Fprintf(w, "hello box") } func login(w http.ResponseWriter, r *http.Request) { //打印请求的方法 fmt.Println("method", r.Method) if r.Method == "GET" { //如果请求方法为get显示login.html,并相应给前端 t, _ := template.ParseFiles("login.html") t.Execute(w, nil) } else { //否则走打印输出post接受的参数username和password fmt.Println(r.PostFormValue("username")) fmt.Println(r.PostFormValue("password")) } } func main() { //监听 / 走sayHelloName http.HandleFunc("/", sayHelloName) //路由控制/login 走login方法 http.HandleFunc("/login", login) err := http.ListenAndServe(":8081", nil) if err != nil { log.Fatal("ListenAndServe:", err) } } -------------------------------------------------- <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>go 登录test</title> </head> <body> <form method="POST" action="/login"> <input type="text" name="username" /> <input type="text" name="password" /> <input type="submit" value="登录"> </form> </body> </html>

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-09-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 利志分享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档