Difference between http.Handle and http.HandleFunc?

后端 未结 3 565
陌清茗
陌清茗 2020-12-07 15:19

The Go docs have the following example for the http package:

http.Handle(\"/foo\", fooHandler)
http.HandleFunc(\"/bar\", func(w http.ResponseWriter, r *http.         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 16:25

    Very correct answers so I won't say much but explain it in simple terms:

    Problem: I want to create an object (type) that responds to HTTP requests.

    Solution: use http.Handle and the object you create should implement ServeHTTP interface from the HTTP package.

    Problem: I want a function to respond to my HTTP request.

    Solution: Use http.HandleFunc for that and register your function with the HTTP server.

提交回复
热议问题