[Go] 自定义一些http Server的参数 用到了interface的一些特性
发布时间:2021-05-21 06:48:21 所属栏目:大数据 来源: https://www.jb51.cc
导读:在一个http服务中? , 如果要定义一些参数比如读超时时间 , 写超时时间 , 那么用最简单的http.ListenAndServe 就不能实现了 需要自己实例化http.Server结构体 ,实例化完成以后 , 之前的路由怎么加进去又是一个问题 http.Server中处理请求响应是通过属性
在一个http服务中? , 如果要定义一些参数比如读超时时间 , 写超时时间 , 那么用最简单的http.ListenAndServe 就不能实现了 需要自己实例化http.Server结构体 ,实例化完成以后 , 之前的路由怎么加进去又是一个问题 http.Server中处理请求响应是通过属性里的Handler来完成的 , 而属性里的Handler是一个interface接口类型 , 必须实现的方法是ServeHTTP(ResponseWriter,*Request) 正好ServeMux这个处理路由的结构体实现了ServeHTTP(ResponseWriter,*Request)方法 , 那么就能直接把这个结构体加进去 log.Println("listen on 8080...rngo: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(编辑:北几岛) |