go defer异常处理
go中的 defer函数相当于C++ 中的try catch函数,可以捕获到程序异常。go http handleFunc中怎样进行异常捕获呢?
package main | |
import ( | |
“fmt” | |
“net/http” | |
“reflect” | |
“runtime” | |
) | |
func hello(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, “Hello!”) | |
} | |
func log(h http.HandlerFunc) http.HandlerFunc { | |
return func(w http.ResponseWriter, r *http.Request) { | |
name := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name() | |
fmt.Println(“Handler function called – “ + name) | |
h(w, r) | |
} | |
} | |
func main() { | |
server := http.Server{ | |
Addr: “127.0.0.1:8080”, | |
} | |
http.HandleFunc(“/hello”, log(hello)) | |
server.ListenAndServe() | |
} |
扫描下方二维码,关注业余草微信公众号,回复“FFmpeg”关键词,获取 FFmpeg 视频教程!