我讀的書,而我最瞭解,我有以下配置的難度「要走的路。」哪裏包裹HandleFunc從獲得responseWriter和要求?
筆者提到的方式在一個封閉該負責恐慌像這樣包裝一個HandleFunc:
func Index(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, "<h2>Index</h2>")
}
func logPanics(function HandleFunc) HandleFunc {
return func(w http.ResponseWriter, req *http.Request) {
defer func() {
if err := recover(); err != nil {
log.Printf("[%v] caught panic: %v", req.RemoteAddr, err)
}
}()
function(w, req) // Where do w and req come from?
}
}
後來,這是通過調用:
http.HandleFunc("/", logPanics(Index))
我瞭解大部分,但不知道如何使用W和REQ
function(w, req)
得到它們的值?我是否理解正確,在回覆聲明中的w和req
return func(w http.ResponseWriter, req *http.Request)
是完全不同的嗎?然後我想知道,w和req如何獲得他們的價值。我希望有人能對我的問題提供一些線索,因爲我真的想了解發生了什麼事情,而不僅僅是複製和粘貼上。