golang 在http中的异常处理

2020-06-23 15:13:36 浏览数 (1)

代码语言:javascript复制
func logPanics(handle http.HandlerFunc) http.HandlerFunc{
    return  func(write http.ResponseWriter,request *http.Request){
        defer func(){
            if x := recover(); x!=nil{
                log.Printf("[%v] caught panic:%v",request.RemoteAddr,x)
            }
        }()
        handle(write, request)
    }

}

0 人点赞