[Go] 自定义一些http Server的参数 用到了interface的一些特性

前端之家收集整理的这篇文章主要介绍了[Go] 自定义一些http Server的参数 用到了interface的一些特性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在一个http服务中  , 如果要定义一些参数比如读超时时间 , 写超时时间 , 那么用最简单的http.ListenAndServe 就不能实现了

需要自己实例化http.Server结构体 ,实例化完成以后 , 之前的路由怎么加进去又是一个问题

http.Server中处理请求响应是通过属性里的Handler来完成的 , 而属性里的Handler是一个interface接口类型 , 必须实现的方法是ServeHTTP(ResponseWriter,*Request)

正好ServeMux这个处理路由的结构体实现了ServeHTTP(ResponseWriter,*Request)方法 , 那么就能直接把这个结构体加进去

    log.Println("listen on 8080...\r\ngo:http://127.0.0.1:8080")
    mux:=&http.ServeMux{}
    //根路径
    mux.HandleFunc(/,controller.ActionIndex)
    邮件
    mux.HandleFunc(/list登陆界面
    mux.HandleFunc(/loginhttp.Server{
        Addr:           :808030 * time.Second,WriteTimeout:   1 << 20自定义参数 

猜你在找的Go相关文章