我试图从 Golang 文档中进行此练习:https://go.dev/doc/articles/wiki/,但我不明白某些内容。在文章的第二部分中,当我们开始使用“net/http”包时,我们写了这个(我在这里留下了更完整的代码: https: //go.dev/doc/articles/wiki/part2.go):
func viewHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[len("/view/"):]
p, _ := loadPage(title)
fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body)
}
func main() {
http.HandleFunc("/view/", viewHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么 viewHandler 位于 http.HandleFunc 的参数中而没有上面定义的两个参数。因为,viewHandler的定义中有两个参数:w和r?什么时候/谁完成?