go http简单的表单处理
2024-08-26 16:47:42
//表单处理
package main
import (
"net/http"
"io"
"fmt"
"log"
)
const form = `<html><body>
<form action="#" method="post" />
<input type="text" name="name" />
<input type="text" name="pass" />
<input type="submit" value="submit" />
</form>
</body></html>`
func deal2 (w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
//Method 代表指定的http方法
switch r.Method {
case "GET":
//io.WriteString(w, s string) 将字符串的s写入w中
io.WriteString(w, form)
case "POST":
//func (*Request) PostFormValue
//PostFormValue返回key为键查询r.PostForm字段得到结果[]string切片的第一个值
io.WriteString(w, r.PostFormValue("name"))
io.WriteString(w, "\n")
io.WriteString(w, r.PostFormValue("pass"))
}
}
//panic 处理
func logsPanic (handle http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func () {
if x := recover(); x != nil {
log.Printf("[%v] catch panic:%v", r.RemoteAddr, x)
}
}()
handle(w, r)
}
}
func main () {
http.HandleFunc("/test1", func (w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>hello world</h1>")
})
http.HandleFunc("/test2", logsPanic(deal2))
if err := http.ListenAndServe("localhost:10010", nil); err != nil {
fmt.Println("listen server failed, error:", err)
return
}
}
最新文章
- PHP钩子机制
- 你应了解的4种JS设计模式
- python 连接 mysql
- linux BASH shell设置字体与背景颜色
- 【转】protobuf2.5.0在<;delete [] elements_;>;crash的问题。
- PHP 的try catch 报错捕获机制
- fragment Trying to instantiate a class com.example.testhuanxindemo.MyFragment that is not a Fragmen
- IOS开发-表视图LV3导航控制器
- 【原创】leetCodeOj --- Fraction to Recurring Decimal 解题报告
- cocos2d-js 帧序列动画
- 团队作业4——第一次项目冲刺 tHe LaSt dAy
- 前端面试题系列(1):doctype作用 标准模式与兼容模式
- Android布局优化:include 、merge、ViewStub的详细总结
- 如何在当前目录下快速打开cmd(或者以管理员的身份打开)
- 安装Percona版本的MySQL主从复制
- NetBeans GUI tests on Jenkins + Windows (转)
- Python模块 2
- Python unittest discover()方法与执行顺序补充
- 简述nginx(1)
- 关于asyncio知识(四)