//表单处理
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
    }
}

  

最新文章

  1. PHP钩子机制
  2. 你应了解的4种JS设计模式
  3. python 连接 mysql
  4. linux BASH shell设置字体与背景颜色
  5. 【转】protobuf2.5.0在&lt;delete [] elements_;&gt;crash的问题。
  6. PHP 的try catch 报错捕获机制
  7. fragment Trying to instantiate a class com.example.testhuanxindemo.MyFragment that is not a Fragmen
  8. IOS开发-表视图LV3导航控制器
  9. 【原创】leetCodeOj --- Fraction to Recurring Decimal 解题报告
  10. cocos2d-js 帧序列动画
  11. 团队作业4——第一次项目冲刺 tHe LaSt dAy
  12. 前端面试题系列(1):doctype作用 标准模式与兼容模式
  13. Android布局优化:include 、merge、ViewStub的详细总结
  14. 如何在当前目录下快速打开cmd(或者以管理员的身份打开)
  15. 安装Percona版本的MySQL主从复制
  16. NetBeans GUI tests on Jenkins + Windows (转)
  17. Python模块 2
  18. Python unittest discover()方法与执行顺序补充
  19. 简述nginx(1)
  20. 关于asyncio知识(四)

热门文章

  1. vue项目用户登录状态管理,vuex+localStorage实现
  2. 【深度学习】关于Adam
  3. oralce 超过1亿条数据的数据库表清理实践
  4. HTMLPage测试js通过ajax调用
  5. GB28181技术基础之1 - SIP协议
  6. IDEA使用本机指定的java环境
  7. Flutter 中AlertDialog确认提示弹窗
  8. realsense d435i問題太多了
  9. Fiddler抓包显示请求时延
  10. 123457123457#0#----com.MC.konglongtianse222----前拼后广--恐龙填色mc-mc1111